工廠模式包含三種模式:簡(jiǎn)單工廠模式负乡、工廠方法模式和抽象工廠模式。
簡(jiǎn)單工廠模式
- 定義
簡(jiǎn)單工廠模式:由一個(gè)工廠類根據(jù)傳入的參數(shù)果录,動(dòng)態(tài)決定應(yīng)該創(chuàng)建哪一個(gè)產(chǎn)品類(這些產(chǎn)品類繼承自一個(gè)父類或接口)的實(shí)例涛贯。
要素:工廠(Creator)坝辫、抽象產(chǎn)品(Product)、具體產(chǎn)品(Concrete Product)
工廠:負(fù)責(zé)實(shí)現(xiàn)創(chuàng)建所有實(shí)例的內(nèi)部邏輯汹粤。工廠類的創(chuàng)建產(chǎn)品類的方法可以被外界直接調(diào)用命斧,創(chuàng)建所需的產(chǎn)品對(duì)象。
抽象產(chǎn)品:所創(chuàng)建的所有產(chǎn)品對(duì)象的父類嘱兼,它負(fù)責(zé)描述所有實(shí)例共有的公共接口国葬。
具體產(chǎn)品:創(chuàng)建目標(biāo)。 - 類圖
- Class
// 披薩:抽象的產(chǎn)品或抽象的算法
// 此抽象類提供了某些默認(rèn)的基本做法
public abstract class Pizza {
public static final int CheesePizza = 1;
public static final int ClamPizza = 2;
public String name;
protected void prepare(){
System.out.println("Preparing " + name);
}
protected void bake(){
System.out.println("Bake for 25 minutes.");
}
protected void cut(){
System.out.println("Cutting the pizza into diagonal slices.");
}
protected void box(){
System.out.println("Place pizza in official PizzaStore box.");
}
}
// 奶酪披薩
public class CheesePizza extends Pizza {
public CheesePizza(){
this.name = "Cheese Pizza";
}
}
// 蛤蜊披薩
public class ClamPizza extends Pizza {
public ClamPizza(){
this.name = "Clam Pizza";
}
}
// 工廠類
public class SimplePizzaFactory {
public static Pizza createPizza(int pizzaType){
Pizza pizza = null;
switch (pizzaType){
case Pizza.CheesePizza:
pizza = new CheesePizza();
break;
case Pizza.ClamPizza:
pizza = new ClamPizza();
break;
}
return pizza;
}
}
// 披薩商店 :可看做客戶端
// 對(duì)客戶端來(lái)說(shuō)芹壕,去除了與具體產(chǎn)品的依賴汇四。
public class PizzaStore {
public static Pizza orderPizza(int pizzaType){
Pizza pizza;
pizza = SimplePizzaFactory.createPizza(pizzaType);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
- 測(cè)試
// 訂購(gòu)一份奶酪披薩
PizzaStore.orderPizza(Pizza.CheesePizza);
// 訂購(gòu)一份蛤蜊披薩
PizzaStore.orderPizza(Pizza.ClamPizza);
工廠方法模式
- 定義
工廠方法模式:定義了一個(gè)創(chuàng)建對(duì)象的接口,但由子類決定要實(shí)例化的類是哪一個(gè)踢涌。工廠方法讓類把實(shí)例化推遲到子類通孽。 - 類圖
- Class
// 創(chuàng)建者通常會(huì)包含依賴于抽象產(chǎn)品的代碼,而具體產(chǎn)品由子類制造睁壁,創(chuàng)建者不需要知道在制造哪種具體產(chǎn)品背苦。
public abstract class PizzaStore {
public Pizza orderPizza(int pizzaType){
Pizza pizza;
pizza = createPizza(pizzaType);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
// 工廠方法用來(lái)處理對(duì)象的創(chuàng)建互捌,并將這樣的行為封裝在子類中。
public abstract Pizza createPizza(int pizzaType);
}
// 紐約披薩店(具體創(chuàng)建者)
public class NewYorkPizzaStore extends PizzaStore {
// 實(shí)現(xiàn)createPizza方法行剂,創(chuàng)建紐約本地風(fēng)味的披薩秕噪。
@Override
public Pizza createPizza(int pizzaType) {
Pizza pizza = null;
switch (pizzaType){
case Pizza.NewYork_CheesePizza:
pizza = new NewYorkCheesePizza();
break;
case Pizza.NewYork_ClamPizza:
pizza = new NewYorkClamPizza();
break;
}
return pizza;
}
}
// 芝加哥披薩店(具體創(chuàng)建者)
public class ChicagoPizzaStore extends PizzaStore {
// 實(shí)現(xiàn)createPizza方法,創(chuàng)建芝加哥本地風(fēng)味的披薩硼讽。
@Override
public Pizza createPizza(int pizzaType) {
Pizza pizza = null;
switch (pizzaType){
case Pizza.Chicago_CheesePizza:
pizza = new ChicagoCheesePizza();
break;
case Pizza.Chicago_ClamPizza:
pizza = new ChicagoClamPizza();
break;
}
return pizza;
}
}
// 披薩:抽象的產(chǎn)品或抽象的算法
// 此抽象類提供了某些默認(rèn)的基本做法
public abstract class Pizza {
public static final int NewYork_CheesePizza = 1;
public static final int NewYork_ClamPizza = 2;
public static final int Chicago_CheesePizza = 3;
public static final int Chicago_ClamPizza = 4;
public String name;
protected void prepare(){
System.out.println("Preparing " + name);
}
protected void bake(){
System.out.println("Bake for 25 minutes.");
}
protected void cut(){
System.out.println("Cutting the pizza into diagonal slices.");
}
protected void box(){
System.out.println("Place pizza in official PizzaStore box.");
}
}
// 芝加哥奶酪披薩(具體產(chǎn)品)
public class ChicagoCheesePizza extends Pizza {
public ChicagoCheesePizza(){
this.name = "Chicago style cheese pizza";
}
}
// 芝加哥蛤蜊披薩(具體產(chǎn)品)
public class ChicagoClamPizza extends Pizza {
public ChicagoClamPizza(){
this.name = "Chicago style clam pizza";
}
}
// 紐約奶酪披薩(具體產(chǎn)品)
public class NewYorkCheesePizza extends Pizza {
public NewYorkCheesePizza(){
this.name = "New York style cheese pizza";
}
}
// 紐約蛤蜊披薩(具體產(chǎn)品)
public class NewYorkClamPizza extends Pizza {
public NewYorkClamPizza() {
this.name = "New York clam pizza";
}
}
- 測(cè)試
PizzaStore newYorkPizzaStore = new NewYorkPizzaStore();
PizzaStore chicagoPizzaStore = new ChicagoPizzaStore();
// 訂購(gòu)一份紐約風(fēng)味的奶酪披薩
newYorkPizzaStore.orderPizza(Pizza.NewYork_CheesePizza);
// 訂購(gòu)一份芝加哥風(fēng)味的蛤蜊披薩
chicagoPizzaStore.orderPizza(Pizza.Chicago_ClamPizza);
抽象工廠模式
- 定義
抽象工廠:提供一個(gè)接口巢价,用于創(chuàng)建相關(guān)或依賴對(duì)象的家族,而不需要明確指定具體類固阁。
抽象工廠允許客戶使用抽象的接口來(lái)創(chuàng)建一組相關(guān)的產(chǎn)品壤躲,而不需要知道(或關(guān)心)實(shí)際產(chǎn)出的具體產(chǎn)品是什么。這樣一來(lái)备燃,客戶就從具體的產(chǎn)品中被解耦碉克。 - 類圖
- Class
// 為原料工廠定義一個(gè)接口,這個(gè)接口負(fù)責(zé)創(chuàng)建所有的原料并齐。
public interface PizzaIngredientFactory {
// 生產(chǎn)面團(tuán)
public Dough createDough();
// 生產(chǎn)醬汁
public Sauce createSauce();
}
// 紐約披薩原料工廠
public class NewYorkPizzaIngredientFactory implements PizzaIngredientFactory {
@Override
public Dough createDough() {
return new ThinCrustDough();
}
@Override
public Sauce createSauce() {
return new MarinaraSauce();
}
}
// 芝加哥披薩原料工廠
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {
@Override
public Dough createDough() {
return new ThickCrustDough();
}
@Override
public Sauce createSauce() {
return new PlumTomatoSauce();
}
}
// 面團(tuán)(原料)
public abstract class Dough {
public String name;
}
// 醬汁(原料)
public abstract class Sauce {
public String name;
}
// 薄脆面團(tuán)
public class ThinCrustDough extends Dough {
public ThinCrustDough(){
this.name = "New york style thin crust dough";
}
}
// 厚餅面團(tuán)
public class ThickCrustDough extends Dough {
public ThickCrustDough(){
this.name = "Chicago style thick crust dough";
}
}
// 意大利紅醬
public class MarinaraSauce extends Sauce {
public MarinaraSauce(){
this.name = "New york style marinara sauce";
}
}
// 番茄醬
public class PlumTomatoSauce extends Sauce {
public PlumTomatoSauce(){
this.name = "Chicago style plum tomato sauce";
}
}
// 披薩:抽象的產(chǎn)品或抽象的算法
// 此抽象類提供了某些默認(rèn)的基本做法
public abstract class Pizza {
public static final int NewYork_CheesePizza = 1;
public static final int NewYork_ClamPizza = 2;
public static final int Chicago_CheesePizza = 3;
public static final int Chicago_ClamPizza = 4;
public String name;
public Dough dough;
public Sauce sauce;
public void prepare(){
System.out.println("Preparing " + name);
}
public void bake(){
System.out.println("Bake for 25 minutes.");
}
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 class ChicagoCheesePizza extends Pizza {
private PizzaIngredientFactory ingredientFactory;
public ChicagoCheesePizza(PizzaIngredientFactory ingredientFactory){
this.name = "Chicago style cheese pizza";
this.ingredientFactory = ingredientFactory;
}
@Override
public void prepare() {
super.prepare();
// 生產(chǎn)面團(tuán)
this.dough = ingredientFactory.createDough();
// 生產(chǎn)醬汁
this.sauce = ingredientFactory.createSauce();
System.out.println("Preparing " + dough.name);
System.out.println("Preparing " + sauce.name);
}
}
// 紐約奶酪披薩
public class NewYorkCheesePizza extends Pizza {
private PizzaIngredientFactory ingredientFactory;
public NewYorkCheesePizza(PizzaIngredientFactory ingredientFactory){
this.name = "New York style cheese pizza";
this.ingredientFactory = ingredientFactory;
}
@Override
public void prepare() {
super.prepare();
// 生產(chǎn)面團(tuán)
this.dough = ingredientFactory.createDough();
// 生產(chǎn)醬汁
this.sauce = ingredientFactory.createSauce();
System.out.println("Preparing " + dough.name);
System.out.println("Preparing " + sauce.name);
}
}
- 測(cè)試
// 創(chuàng)建紐約原料工廠
PizzaIngredientFactory pizzaIngredientFactory = new NewYorkPizzaIngredientFactory();
// 創(chuàng)建紐約風(fēng)味披薩
Pizza newYorkCheesePizza = new NewYorkCheesePizza(pizzaIngredientFactory);
newYorkCheesePizza.prepare();
// 創(chuàng)建芝加哥原料工廠
PizzaIngredientFactory ingredientFactory = new ChicagoPizzaIngredientFactory();
// 創(chuàng)建芝加哥風(fēng)味披薩
Pizza chicagoCheesePizza = new ChicagoCheesePizza(ingredientFactory);
chicagoCheesePizza.prepare();