码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式(9)----- 工厂设计模式(抽象工厂模式)

时间:2018-10-04 11:21:16      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:table   slice   工厂设计   mil   asto   soft   str   shc   pat   

我们先来看一张类图:

技术分享图片

技术分享图片

我们来看一下代码: PizzaStore 披萨工厂类

package com.DesignPatterns.ad.factory3;

public abstract class PizzaStore {

 

 

 

 

 

 

 

 

 

 

 

 

}

protected abstract Pizza createPizza(String item);

public Pizza orderPizza(String type) {

    Pizza pizza = createPizza(type);

    System.out.println("--- Making a " + pizza.getName() + " ---");     pizza.prepare();

    pizza.bake();

    pizza.cut();

    pizza.box();

    return pizza;

}

NYPizzaStore纽约披萨工厂类

public class NYPizzaStore extends PizzaStore {

 

    protected Pizza createPizza(String item) {

         Pizza pizza = null;

         PizzaIngredientFactory ingredientFactory =

              new NYPizzaIngredientFactory();

 

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

 

              pizza = new CheesePizza(ingredientFactory);

              pizza.setName("纽约风格的奶酪披萨");

 

         } else if (item.equals("veggie")) {

 

              pizza = new VeggiePizza(ingredientFactory);

              pizza.setName("纽约风格的素食披萨");

 

         } else if (item.equals("clam")) {

 

              pizza = new ClamPizza(ingredientFactory);

              pizza.setName("纽约风格的蛤蜊披萨");

 

         } else if (item.equals("pepperoni")) {

              pizza = new PepperoniPizza(ingredientFactory);

              pizza.setName("纽约风格的意大利香肠披萨");

 

         }

         return pizza;

    }

}

ChicagoPizzaStore芝加哥披萨专卖店

public class ChicagoPizzaStore extends PizzaStore {

    protected Pizza createPizza(String item) {

         Pizza pizza = null;

         PizzaIngredientFactory ingredientFactory =

         new ChicagoPizzaIngredientFactory();

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

              pizza = new CheesePizza(ingredientFactory);

              pizza.setName("芝加哥奶酪披萨");

         } else if (item.equals("veggie")) {

              pizza = new VeggiePizza(ingredientFactory);

              pizza.setName("芝加哥风格的素食披萨");

         } else if (item.equals("clam")) {

              pizza = new ClamPizza(ingredientFactory);               pizza.setName("芝加哥式蛤蜊披萨");

         } else if (item.equals("pepperoni")) {

              pizza = new PepperoniPizza(ingredientFactory);

              pizza.setName("芝加哥风格的意大利香肠披萨");

         }

         return pizza;

    }

}

PizzaIngredientFactory原料工厂接口类(抽象工厂模式)

public interface PizzaIngredientFactory {

    public Dough createDough();// 创建面团

    public Sauce createSauce();// 创建酱汁

    public Cheese createCheese();// 创建奶酪

    public Veggies[] createVeggies();// 创建蔬菜

    public Pepperoni createPepperoni();// 创建意大利香肠     public Clams createClam();// 创建蛤

}

NYPizzaIngredientFactory纽约原料工厂实现类(抽象工厂模式)

public class NYPizzaIngredientFactory implements PizzaIngredientFactory {

 

    public Dough createDough() {

         return new ThinCrustDough();

    }

 

    public Sauce createSauce() {

         return new MarinaraSauce();

    }

 

    public Cheese createCheese() {

         return new ReggianoCheese();

    }

 

    public Veggies[] createVeggies() {

         Veggies veggies[] = { new Garlic(), new Onion(), new Mushroom(), new RedPepper() };

         return veggies;

    }

 

    public Pepperoni createPepperoni() {

         return new SlicedPepperoni();     }

    public Clams createClam() {

         return new FreshClams();

    }

}

ChicagoPizzaIngredientFactory芝加哥原料工厂实现类(抽象工厂模式)

public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {

    public Dough createDough() {

         return new ThickCrustDough();     }

    public Sauce createSauce() {

         return new PlumTomatoSauce();

    }

 

 

 

 

 

 

 

 

 

 

 

 

 

}

public Cheese createCheese() {

    return new MozzarellaCheese();

}

public Veggies[] createVeggies() {

    Veggies veggies[] = { new BlackOlives(), new Spinach(), new Eggplant() };     return veggies;

}

public Pepperoni createPepperoni() {     return new SlicedPepperoni();

}

public Clams createClam() {

    return new FrozenClams();

}

Pizza类

public abstract class Pizza {

    String name;

    Dough dough;

    Sauce sauce;

    Veggies veggies[];

    Cheese cheese;

    Pepperoni pepperoni;     Clams clam;

    abstract void prepare();

    void bake() {

         System.out.println("在350度下烤25分钟");

    }

    void cut() {

         System.out.println("把披萨切成对角线");

    }

    void box() {

         System.out.println("将披萨放入正式的披萨店");

    }

    void setName(String name) {

         this.name = name;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}

String

 

}

public

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

getName() {

return name;

String toString() {

StringBuffer result = new StringBuffer(); result.append("---- " + name + " ----\n"); if (dough != null) {

         result.append(dough);

         result.append("\n");

}

if (sauce != null) {

         result.append(sauce);

         result.append("\n");

}

if (cheese != null) {

         result.append(cheese);

         result.append("\n");

}

if (veggies != null) {

         for (int i = 0; i < veggies.length; i++) {

              result.append(veggies[i]);

              if (i < veggies.length - 1) {

                   result.append(", ");

              }

         }

         result.append("\n");

}

if (clam != null) {

         result.append(clam);

         result.append("\n");

}

if (pepperoni != null) {     result.append(pepperoni);

         result.append("\n");

}

return result.toString();

}

}

纽约店来创建披萨Test

public static void main(String[] args) {

         PizzaStore nyStore = new NYPizzaStore();

         Pizza pizza = nyStore.orderPizza("cheese");

         // 1,找到PizzaStore抽象类中来,因为这个时候nyStore是NYPizzaStore

         // 2,调用了NYPizzaStore的createPizza方法

         // 3,在NYPizzaStore的方法中有NYPizzaIngredientFactory实例,所以这个时候调用了

         System.out.println("伊桑下令 " + pizza + "=======");

抽象的披萨店PizzaStore

public abstract class PizzaStore {

 

    protected abstract Pizza createPizza(String item);

 

    public Pizza orderPizza(String type) {

         Pizza pizza = createPizza(type);

         System.out.println("--- Making a " + pizza.getName() + " ---");

         pizza.prepare();

         pizza.bake();

         pizza.cut();

         pizza.box();

         return pizza;

    }

}

纽约的披萨店NYPizzaStore

public class NYPizzaStore extends PizzaStore {

 

    protected Pizza createPizza(String item) {

         Pizza pizza = null;

         PizzaIngredientFactory ingredientFactory =

              new NYPizzaIngredientFactory();

 

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

 

              pizza = new CheesePizza(ingredientFactory);

              pizza.setName("纽约风格的奶酪披萨");

 

         } else if (item.equals("veggie")) {

 

              pizza = new VeggiePizza(ingredientFactory);

              pizza.setName("纽约风格的素食披萨");

 

         } else if (item.equals("clam")) {

 

              pizza = new ClamPizza(ingredientFactory);

              pizza.setName("纽约风格的蛤蜊披萨");

 

         } else if (item.equals("pepperoni")) {

              pizza = new PepperoniPizza(ingredientFactory);

              pizza.setName("纽约风格的意大利香肠披萨");

 

         }

         return pizza;

    }

}

抽象纽约,芝加哥披萨原料类PizzaIngredientFactory

public interface PizzaIngredientFactory {

    public Dough createDough();// 创建面团

    public Sauce createSauce();// 创建酱汁

    public Cheese createCheese();// 创建奶酪

    public Veggies[] createVeggies();// 创建蔬菜

    public Pepperoni createPepperoni();// 创建意大利香肠

    public Clams createClam();// 创建蛤

}

具体的纽约原料披萨类NYPizzaIngredientFactory

public class NYPizzaIngredientFactory implements PizzaIngredientFactory {

 

    public Dough createDough() {

         return new ThinCrustDough();

    }

 

    public Sauce createSauce() {

         return new MarinaraSauce();

    }

 

    public Cheese createCheese() {

         return new ReggianoCheese();

    }

 

    public Veggies[] createVeggies() {

         Veggies veggies[] = { new Garlic(), new Onion(), new Mushroom(), new RedPepper() };

         return veggies;

    }

 

    public Pepperoni createPepperoni() {

         return new SlicedPepperoni();     }

    public Clams createClam() {

         return new FreshClams();

    }

}

纽约,芝加哥面团接口类Dough

public interface Dough {     public String toString();

}

纽约面团实现类ThinCrustDough

public class ThinCrustDough implements Dough {

    public String toString() {

         return "薄的地壳面团";

    }

}

纽约,芝加哥蕃茄接口类Sauce

public interface Sauce {     public String toString();

}

纽约番茄接口实现类MarinaraSauce

public class MarinaraSauce implements Sauce {

    public String toString() {

         return "加番茄酱";

    }

}

纽约,芝加哥奶酪接口类Cheese

public interface Cheese {     public String toString();

}

纽约奶酪接口实现类MozzarellaCheese

public class ReggianoCheese implements Cheese {

    public String toString() {

         return "雷奇亚干酪奶酪";

    }

}

芝加哥来创建披萨test实现类

public static void main(String[] args) {

         PizzaStore chicagoStore = new ChicagoPizzaStore();

         // 1,找到PizzaStore抽象类中来,因为这个时候nyStore是NYPizzaStore

         // 2,调用了NYPizzaStore的createPizza方法

         // 3,在NYPizzaStore的方法中有NYPizzaIngredientFactory实例,所以这个时候调用了

         Pizza pizza = chicagoStore.orderPizza("cheese");

         System.out.println("乔尔下令 " + pizza + "=======");

抽象的披萨店PizzaStore

public abstract class PizzaStore {

 

    protected abstract Pizza createPizza(String item);

 

    public Pizza orderPizza(String type) {

         Pizza pizza = createPizza(type);

         System.out.println("--- Making a " + pizza.getName() + " ---");

         pizza.prepare();

         pizza.bake();

         pizza.cut();

         pizza.box();

         return pizza;

    }

}

ChicagoPizzaStore芝加哥原料工厂类

public class ChicagoPizzaStore extends PizzaStore {

    protected Pizza createPizza(String item) {

         Pizza pizza = null;

         PizzaIngredientFactory ingredientFactory =

         new ChicagoPizzaIngredientFactory();

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

              pizza = new CheesePizza(ingredientFactory);

              pizza.setName("芝加哥奶酪披萨");

         } else if (item.equals("veggie")) {

              pizza = new VeggiePizza(ingredientFactory);

              pizza.setName("芝加哥风格的素食披萨");

         } else if (item.equals("clam")) {

              pizza = new ClamPizza(ingredientFactory);               pizza.setName("芝加哥式蛤蜊披萨");

         } else if (item.equals("pepperoni")) {

              pizza = new PepperoniPizza(ingredientFactory);

              pizza.setName("芝加哥风格的意大利香肠披萨");

         }

         return pizza;

    }

}

抽象纽约,芝加哥披萨原料类PizzaIngredientFactory

public interface PizzaIngredientFactory {

    public Dough createDough();// 创建面团

    public Sauce createSauce();// 创建酱汁

    public Cheese createCheese();// 创建奶酪

    public Veggies[] createVeggies();// 创建蔬菜

    public Pepperoni createPepperoni();// 创建意大利香肠

}

public Clams createClam();// 创建蛤

芝加哥原料工厂类ChicagoPizzaIngredientFactory

public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {

    public Dough createDough() {

         return new ThickCrustDough();     }

    public Sauce createSauce() {

         return new PlumTomatoSauce();     }

    public Cheese createCheese() {

         return new MozzarellaCheese();     }

    public Veggies[] createVeggies() {

         Veggies veggies[] = { new BlackOlives(), new Spinach(), new Eggplant() };

         return veggies;

    }

    public Pepperoni createPepperoni() {

         return new SlicedPepperoni();     }

    public Clams createClam() {

         return new FrozenClams();

    }

}

纽约,芝加哥面团接口类Dough

public interface Dough {     public String toString();

}

纽约面团实现类ThickCrustDough

public class ThickCrustDough implements Dough {

    public String toString() {

         return "厚皮面团";

    }

}

纽约,芝加哥蕃茄接口类Sauce

public interface Sauce {     public String toString();

}

纽约番茄接口实现类MarinaraSauce

public class PlumTomatoSauce implements Sauce {

    public String toString() {

         return "蕃茄酱配梅子蕃茄";

    }

}

纽约,芝加哥奶酪接口类Cheese

public interface Cheese {     public String toString();

}

纽约奶酪接口实现类MozzarellaCheese

public class MozzarellaCheese implements Cheese {

    public String toString() {

         return "马苏里拉奶酪丝";

    }

}

抽象工厂模式的定义

技术分享图片

可不可以这样理解抽象工厂是比工厂更抽象的工厂,以前的工厂方法中海油具体的实现,而现在抽象工厂中全部都是抽象的方法。

 

技术分享图片

技术分享图片

 

其实还可以这样理解,工厂定义是让子类来决定自己到底是谁,可能是披萨1,披萨2,披萨3等等。接下来开始做披萨该做的事情。而抽象的则是不需要返回具体了类,比如说现在返回披萨的总和,又返回面包的总和,当我用的时候可能是披萨1+面包1,披萨1+面包2,披萨2+面包1,等等,更倾向于组合。关注点不一样,工厂关注与返回当前的产品的实例,而抽象则关注与返回一组数据的组合。

当产品只有一个的时候抽象工厂变成工厂,当产品有多个的时候工厂变成抽象工厂。

技术分享图片

技术分享图片

kk

设计模式(9)----- 工厂设计模式(抽象工厂模式)

标签:table   slice   工厂设计   mil   asto   soft   str   shc   pat   

原文地址:https://www.cnblogs.com/qingruihappy/p/9739648.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!