1. 程式人生 > 其它 >Java面試題目!【原始碼分析設計模式 2

Java面試題目!【原始碼分析設計模式 2

Java面試題目!【原始碼分析設計模式 2

六、程式碼例項


1、UML類圖

2、程式碼架構

3、具體程式碼

(1)order包包


package com.guor.abstractFactory.order;



import com.guor.abstractFactory.pizza.Pizza;



//一個抽象工廠模式的抽象層(介面)

public interface AbsFactory {

	//讓下面的工廠子類來 具體實現

	public Pizza createPizza(String orderType);

}

package com.guor.abstractFactory.order;

import com.guor.abstractFactory.pizza.BJCheesePizza;

import com.guor.abstractFactory.pizza.BJPepperPizza;

import com.guor.abstractFactory.pizza.Pizza;

//這是工廠子類

public class BJFactory implements AbsFactory {

@Override

public Pizza createPizza(String orderType) {

	System.out.println("~使用的是抽象工廠模式~");

	Pizza pizza = null;

	if(orderType.equals("cheese")) {

		pizza = new BJCheesePizza();

	} else if (orderType.equals("pepper")){

		pizza = new BJPepperPizza();

	}

	return pizza;

}

}


package com.guor.abstractFactory.order;



import com.guor.abstractFactory.pizza.LDCheesePizza;

import com.guor.abstractFactory.pizza.LDPepperPizza;

import com.guor.abstractFactory.pizza.Pizza;



public class LDFactory implements AbsFactory {

	@Override

	public Pizza createPizza(String orderType) {

		System.out.println("~使用的是抽象工廠模式~");

		Pizza pizza = null;

		if (orderType.equals("cheese")) {

			pizza = new LDCheesePizza();

		} else if (orderType.equals("pepper")) {

			pizza = new LDPepperPizza();

		}

		return pizza;

	}

} 

package com.guor.abstractFactory.order;

import com.guor.abstractFactory.pizza.Pizza;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class OrderPizza {

AbsFactory factory;

// 構造器

public OrderPizza(AbsFactory factory) {

	setFactory(factory);

}



private void setFactory(AbsFactory factory) {

	Pizza pizza = null;

	String orderType = ""; // 使用者輸入

	this.factory = factory;

	do {

		orderType = getType();

		// factory 可能是北京的工廠子類,也可能是倫敦的工廠子類

		pizza = factory.createPizza(orderType);

		if (pizza != null) { // 訂購ok

			pizza.prepare();

			pizza.bake();

			pizza.cut();

			pizza.box();

		} else {

			System.out.println("訂購失敗");

			break;

		}

	} while (true);

}



// 寫一個方法,可以獲取客戶希望訂購的披薩種類

private String getType() {

	try {

		BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));

		System.out.println("input pizza 種類:");

		String str = strin.readLine();

		return str;

	} catch (IOException e) {

		e.printStackTrace();

		return "";

	}

}

}


package com.guor.abstractFactory.order;



public class PizzaStore {

	public static void main(String[] args) {

		new OrderPizza(new BJFactory());

	}

} 

```



(2)pizza包包



```

package com.guor.abstractFactory.pizza;



public class BJCheesePizza extends Pizza {

	@Override

	public void prepare() {

		setName("北京的乳酪pizza");

		System.out.println("北京的乳酪pizza 準備原材料");

	}

} 

package com.guor.abstractFactory.pizza;

public class BJPepperPizza extends Pizza {

@Override

public void prepare() {

	setName("北京的胡椒pizza");

	System.out.println(" 北京的胡椒pizza 準備原材料");

}

}


package com.guor.abstractFactory.pizza;



public class LDCheesePizza extends Pizza{

	@Override

	public void prepare() {

		setName("倫敦的乳酪pizza");

		System.out.println(" 倫敦的乳酪pizza 準備原材料");

	}

} 

package com.guor.abstractFactory.pizza;

public class LDPepperPizza extends Pizza{

總結

如果你選擇了IT行業並堅定的走下去,這個方向肯定是沒有一絲問題的,這是個高薪行業,但是高薪是憑自己的努力學習獲取來的,這次我把P8大佬用過的一些學習筆記(pdf)都整理在本文中了,如果你有需要的話,請一定點贊分享本文,然後點選這裡獲取免費下載方式!

《Java中高階核心知識全面解析》

小米商場專案實戰,別再擔心面試沒有實戰專案: