設計模式: 自己手動寫一個工廠模式(整合工廠方法模式和抽象工廠模式)

 工廠模式: 所有工廠模式都用來封裝對象的創建。工廠方法模式通過讓子類決定該創建的對象是什麼,來達到將對象的創建過程封裝的目的。

工廠方法模式定義了一個創建對象的接口,但由子類決定要實例化的類是哪一個。工廠方法讓類把實例化推遲到子類。

抽象工廠模式提供一個接口,用來創建相關或依賴對象的家族,而不需要明確指定具體類。

工廠方法使用繼承:把對象的創建委託給子類,子類實現工廠方法來創建對象。

抽象工廠使用對象組合: 對象的創建被實現在工廠接口所暴露出來的方法中。抽象工廠創建相關的對象家族,而不需要依賴它們的具體類。


下面是工廠方法的類圖



下面是工程的結構圖


AbstractFactorys包下的類主要體現抽象工廠模式的運用

factoryMethods 包下的類主要體現工廠方法模式的運用


源代碼如下:

先給出 factoryMethods 下的代碼

package factoryMethods;

import AbstractFactorys.PizzaIngredientFactory;

/**
 * 披薩商店抽象類 超類
 * @author Arvon
 *
 */
public abstract class PizzaStore {
	PizzaIngredientFactory ingredientFactory;
	public void orderPizza(String type,PizzaIngredientFactory ingredientFactory){
		Pizza pizza;
		pizza = createPizza(type,ingredientFactory);
		pizza.prepare();
		pizza.bake();
		pizza.cut();
		pizza.box();
	}
	/**
	 * 生產披薩  工廠方法 由子類來實現
	 * @param type
	 * @return
	 */
	public abstract Pizza createPizza(String type,PizzaIngredientFactory ingredientFactory);
	
	
}


package factoryMethods;

import AbstractFactorys.PizzaIngredientFactory;

/**
 * 芝加哥披薩商店 負責生產披薩
 * @author Administrator
 *
 */
public class ChicagoPizzaStore extends PizzaStore {
	
	@Override
	public Pizza createPizza(String type,PizzaIngredientFactory ingredientFactory) {
		if(type.equals(Constant.CHEESE))
			return new ChicagoStyleCheesePizza(ingredientFactory);
		else if(type.equals(Constant.VEGGIE))
			return new ChicagoStyleVeggiePizza(ingredientFactory);
		else return null;
	}

}



package factoryMethods;

import AbstractFactorys.PizzaIngredientFactory;

/**
 * 紐約披薩商店 負責生產披薩
 * @author Administrator
 *
 */
public class NYPizzaStroe extends PizzaStore {

	@Override
	public Pizza createPizza(String type, PizzaIngredientFactory ingredientFactory) {
		if(type.equals(Constant.CHEESE))
			return new NYStyleCheesePizza(ingredientFactory);
		else if(type.equals(Constant.VEGGIE))
			return new NYStyleVeggiePizza();
		else return null;
	}
}


package factoryMethods;

import AbstractFactorys.Cheese;
import AbstractFactorys.Veggie;

/**
 * 產品類 超類
 * @author Administrator
 *
 */
public abstract class Pizza {
	String name;
	Veggie veggie;
	Cheese cheese;
	
	
	public abstract void prepare();

	public void bake() {
		System.out.println("Bake for 25 minutes at 350");
		
	}

	public void cut() {
		System.out.println("Cutting the pizza into diagonal slices");
	}

	public void box() {
		System.out.println("Place pizza in official PizzaStore box");
	}

	public void setName(String name) {
		this.name = name;
	}
	
	

}


package factoryMethods;

import AbstractFactorys.PizzaIngredientFactory;

public class ChicagoStyleCheesePizza extends Pizza {
	PizzaIngredientFactory ingredientFactory;
	public ChicagoStyleCheesePizza(PizzaIngredientFactory ingredientFactory) {
		super();
		// TODO Auto-generated constructor stub
		name = "ChicagoStyleCheesePizza";
		this.ingredientFactory = ingredientFactory;
	}

	@Override
	public void prepare() {
		
	}
	
}

package factoryMethods;

import AbstractFactorys.PizzaIngredientFactory;

public class ChicagoStyleVeggiePizza extends Pizza {
	PizzaIngredientFactory ingredientFactory;
	public ChicagoStyleVeggiePizza(PizzaIngredientFactory ingredientFactory) {
		super();
		// TODO Auto-generated constructor stub
		name = "ChicagoStyleVeggiePizza";
		this.ingredientFactory = ingredientFactory;
	}

	@Override
	public void prepare() {
		// TODO Auto-generated method stub
		cheese = ingredientFactory.createCheese();
		veggie = ingredientFactory.createVeggie();
		System.out.println("Preparing " + name);
		System.out.println(cheese.getName());
		System.out.println(veggie.getName());
	}
	
}


package factoryMethods;

import AbstractFactorys.PizzaIngredientFactory;

public class NYStyleCheesePizza extends Pizza {
	PizzaIngredientFactory ingredientFactory;
	public NYStyleCheesePizza(PizzaIngredientFactory ingredientFactory) {
		super();
		name = "NYStyleCheesePizza";
		this.ingredientFactory = ingredientFactory;
	}

	@Override
	public void prepare() {
		cheese = ingredientFactory.createCheese();
		veggie = ingredientFactory.createVeggie();
		System.out.println("Preparing " + name);
		System.out.println(cheese.getName());
		System.out.println(veggie.getName());
	}
	
}


package factoryMethods;

public class NYStyleVeggiePizza extends Pizza {

	public NYStyleVeggiePizza() {
		super();
		// TODO Auto-generated constructor stub
		name = "NYStyleVeggiePizza";
	}

	@Override
	public void prepare() {
		// TODO Auto-generated method stub
		
	}

}

package factoryMethods;

public interface Constant {
	public static final String CHEESE = "cheese";
	public static final String VEGGIE = "veggie";
}


接下來是AbstractFactorys包裏的類

package AbstractFactorys;
/**
 * 原料生產工廠 抽象工廠
 * @author Administrator
 *
 */
public interface PizzaIngredientFactory {
	public Cheese createCheese();
	public Veggie createVeggie();
	
}


package AbstractFactorys;
/**
 * 原料生產工廠 具體工廠
 * @author Administrator
 *
 */
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {

	@Override
	public Cheese createCheese() {
		return new Cheese(this);
	}

	@Override
	public Veggie createVeggie() {
		return new Veggie(this);
	}

}


package AbstractFactorys;
/**
 * 原料生產工廠 具體工廠
 * @author Administrator
 *
 */
public class NYPizzaIngredientFactory implements PizzaIngredientFactory {

	@Override
	public Cheese createCheese() {
		// TODO Auto-generated method stub
		return new Cheese(this);
	}

	@Override
	public Veggie createVeggie() {
		// TODO Auto-generated method stub
		return new Veggie(this);
	}

}


package AbstractFactorys;
/**
 * 原料類 超類
 * @author Administrator
 *
 */
public class Ingredient {
	String name;

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	
}

package AbstractFactorys;
/**
 * 具體的原料
 * @author Administrator
 *
 */
public class Cheese extends Ingredient {

	public Cheese(PizzaIngredientFactory ingredientFactory) {
		name = "cheese from "+ ingredientFactory.getClass().getSimpleName();
		setName(name);
	}
	
}


package AbstractFactorys;
/**
 * 具體的原料
 * @author Administrator
 *
 */
public class Veggie extends Ingredient {

	public Veggie(PizzaIngredientFactory ingredientFactory) {
		name = "veggie from "+ ingredientFactory.getClass().getSimpleName();
	}

}


測試類

package factoryMethods;

import AbstractFactorys.ChicagoPizzaIngredientFactory;
import AbstractFactorys.NYPizzaIngredientFactory;
import AbstractFactorys.PizzaIngredientFactory;

/**
 * 測試類
 * @author Administrator
 *
 */
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PizzaStore mPizzaStore = new NYPizzaStroe();
		PizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory();
		mPizzaStore.orderPizza("cheese", ingredientFactory);
		System.out.println("-----------------------------------");
		ingredientFactory = new ChicagoPizzaIngredientFactory();
		mPizzaStore = new ChicagoPizzaStore();
		mPizzaStore.orderPizza("veggie", ingredientFactory);
		
	}
	

}

測試結果:

Preparing NYStyleCheesePizza
cheese from NYPizzaIngredientFactory
veggie from NYPizzaIngredientFactory
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
-----------------------------------
Preparing ChicagoStyleVeggiePizza
cheese from ChicagoPizzaIngredientFactory
veggie from ChicagoPizzaIngredientFactory
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章