`
wangpx
  • 浏览: 198549 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

抽象工厂模式读书笔记

阅读更多
提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。抽象工厂允许客户使用抽象的接口来创建一组相关产品,而不需要关心具体实际产出的产品是什么。
总结 :
  1. 所有工厂都是用来封装对象的创建。
  2. 简单工厂,虽然不是真正的设计模式,但是仍然不失为一个简单的方法,可以把客户程序从具体类解耦。
  3. 工厂方法使用继承:把对象的创建委托给子类,子类实现工厂方法来创建对象。
  4. 抽象工厂使用对象组合:对象的创建被实现在工厂接口所暴露出来的方法  中。
  5. 所有工厂模式都是通过减少应用程序和具体类之间的依赖关系促进松耦合。
  6. 工厂方法允许类将实例化延迟到子类进行。
  7. 抽象工厂创建相关的家族,而不需要依赖他们的具体类。 
  8. 依赖倒置原则,指导我们避免依赖具体类型,而要尽量依赖抽象。
  9. 工厂是很有威力的技巧,帮助我们针对抽象编成,而不是针对具体类编成。



package pattern; 

import java.util.ArrayList; 

public abstract class PizzaStore {
 SimplePizzaFactory factory; 

 public PizzaStore() {
 } 

 public PizzaStore(SimplePizzaFactory factory) {
  this.factory = factory;
 } 

 public Pizza orderPizza(String type) {
  Pizza pizza;
  pizza = createPizza(type);
  pizza.prepare();
  pizza.bake();
  pizza.cut();
  pizza.box();
  return pizza;
 } 

 abstract Pizza createPizza(String type);
} 

class NYPizzaStore extends PizzaStore {
 Pizza createPizza(String item) {
  Pizza pizza=null;
  PizzaIngredientFactory ingredientFactory=new NYPizzaIngredientFctory();
  if (item.equals("cheese")) {
   pizza= new CheesePizza(ingredientFactory);
   pizza.setName("New York Style Cheese Pizza");
  } else if (item.equals("pepperoni")) {
   pizza=new PepperoniPizza(ingredientFactory);
   pizza.setName("New York Style pepperoni Pizza");
  } else if (item.equals("clam")) {
   pizza=new ClamPizza(ingredientFactory);
   pizza.setName("New York Style Clame Pizza");
  } else if (item.equals("veggie")) {
   pizza=new VeggiePizza(ingredientFactory);
   pizza.setName("New York Style Veggie Pizza");
  } 
   return pizza;
 }
} 

class ChicagoPizzaStore extends PizzaStore {
 Pizza createPizza(String item) {
  Pizza pizza=null;
  PizzaIngredientFactory ingredientFactory=new NYPizzaIngredientFctory();
  if (item.equals("cheese")) {
   pizza= new CheesePizza(ingredientFactory);
   pizza.setName("New York Style Cheese Pizza");
  } else if (item.equals("pepperoni")) {
   pizza=new PepperoniPizza(ingredientFactory);
   pizza.setName("New York Style pepperoni Pizza");
  } else if (item.equals("clam")) {
   pizza=new ClamPizza(ingredientFactory);
   pizza.setName("New York Style Clame Pizza");
  } else if (item.equals("veggie")) {
   pizza=new VeggiePizza(ingredientFactory);
   pizza.setName("New York Style Veggie Pizza");
  } 
   return pizza;
 }
} 

class CaliforniaPizzaStore extends PizzaStore {
 Pizza createPizza(String item) {
  Pizza pizza=null;
  PizzaIngredientFactory ingredientFactory=new NYPizzaIngredientFctory();
  if (item.equals("cheese")) {
   pizza= new CheesePizza(ingredientFactory);
   pizza.setName("New York Style Cheese Pizza");
  } else if (item.equals("pepperoni")) {
   pizza=new PepperoniPizza(ingredientFactory);
   pizza.setName("New York Style pepperoni Pizza");
  } else if (item.equals("clam")) {
   pizza=new ClamPizza(ingredientFactory);
   pizza.setName("New York Style Clame Pizza");
  } else if (item.equals("veggie")) {
   pizza=new VeggiePizza(ingredientFactory);
   pizza.setName("New York Style Veggie Pizza");
  } 
   return pizza;
 }
} 

abstract class Pizza {
 String name;
 Dough dough;
 Sauce sauce;
 Veggies veggies[];
 Cheese cheese;
 Pepperoni pepperoni;
 Clams clam;
 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 String getName() {
  return name;
 }
 void setName(String name){
  this.name=name;
 }
} 

class CheesePizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public CheesePizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
 }
} 

class NYStyleCheesePizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public NYStyleCheesePizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
 }
} 

class ChicagoStyleCheesePizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public ChicagoStyleCheesePizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
 }
} 

class CaliforniaStyleCheesePizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public CaliforniaStyleCheesePizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
 }
} 

class PepperoniPizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public PepperoniPizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
 }
} 

class NYStylePepperoniPizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public NYStylePepperoniPizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
 }
} 

class ChicagoStylePepperoniPizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public ChicagoStylePepperoniPizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
 }
} 

class CaliforniaStylePepperoniPizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public CaliforniaStylePepperoniPizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
 }
} 

class ClamPizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public ClamPizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
  clam=this.ingredientFactory.createClam();
 }
} 

class NYStyleClamPizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public NYStyleClamPizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
  clam=this.ingredientFactory.createClam();
 }
} 

class ChicagoStyleClamPizz extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public ChicagoStyleClamPizz(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
  clam=this.ingredientFactory.createClam();
 }
} 

class CaliforniaStyleClamPizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public CaliforniaStyleClamPizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
  clam=this.ingredientFactory.createClam();
 }
} 

class VeggiePizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public VeggiePizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
  clam=this.ingredientFactory.createClam();
 }
} 

class NYStyleVeggiePizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public NYStyleVeggiePizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
  clam=this.ingredientFactory.createClam();
 }
} 

class ChicagoStyleVeggiePizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public ChicagoStyleVeggiePizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
  clam=this.ingredientFactory.createClam();
 }
} 

class CaliforniaStyleVeggiePizza extends Pizza {
 PizzaIngredientFactory ingredientFactory;
 public CaliforniaStyleVeggiePizza(PizzaIngredientFactory ingredientFactory) {
  this.ingredientFactory=ingredientFactory;
 }
 void prepare(){
  System.out.println("Preparing " + name);
  dough=this.ingredientFactory.createDough();
  sauce=this.ingredientFactory.createSauce();
  cheese=this.ingredientFactory.createCheese();
  clam=this.ingredientFactory.createClam();
 }
}
interface Dough{
 
}
class ThickCrustDough implements Dough{
 public ThickCrustDough(){
  System.out.println("ThickCrustDough");
 }
}
class ThinCrustDough implements Dough{
 public ThinCrustDough(){
  System.out.println("ThinCrustDough");
 }
}
interface Clams{
 
}
class FrozenClams implements Clams{
 public FrozenClams(){
  System.out.println("FrozenClams");
 }
}
class FreshClams implements Clams{
 public FreshClams(){
  System.out.println("FreshClams");
 }
}
interface Sauce{
 
}
class PlumTomatoSauce implements Sauce{
 public PlumTomatoSauce(){
  System.out.println("PlumTomatoSauce");
 }
}
class MarinaraSauce implements Sauce{
   public MarinaraSauce(){
    System.out.println("MarinaraSauce");
   }
}
 
interface Cheese{ 
}
class MozzarellaCheese implements Cheese{
 public MozzarellaCheese(){
  System.out.println("MozzarellaCheese");
 } 
}
class ReggianoCheese implements Cheese{
 public ReggianoCheese(){
  System.out.println("ReggianoCheese");
 }
}
interface  Pepperoni{
}
class SlicedPepperoni implements Pepperoni{
 public SlicedPepperoni(){
  System.out.println("SlicedPepperoni");
 }
}
interface Veggies{
}
class Garlic implements Veggies{
 public Garlic(){
  System.out.println("Garlic");
 }
}
class Onion implements Veggies{
 public Onion(){
  System.out.println("Onion");
 }
}
class Mushroom implements Veggies{
 public Mushroom(){
  System.out.println("Mushroom");
 }
}
class RedPepper implements Veggies{
 public RedPepper(){
  System.out.println("RedPepper");
 }
}
class BlackOlives implements Veggies{
 public BlackOlives(){
  System.out.println("BlackOlives");
 }
}
class Spinach implements Veggies{
 public Spinach(){
  System.out.println("Spinach");
 }
}
class Eggplant implements Veggies{
 public Eggplant(){
  System.out.println("Eggplant");
 }
}
interface PizzaIngredientFactory{
 public Dough createDough();
 public Sauce createSauce();
 public Cheese createCheese();
 public Veggies[] createVeggies();
 public Pepperoni createPepperoni();
 public Clams createClam();
}
class NYPizzaIngredientFctory 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();
 }
} 

class ChicagoPizzaIngredientFctory implements PizzaIngredientFactory{
 public Dough createDough(){
  return new ThinCrustDough();
 }
 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();
 }
} 

  

package pattern; 

public class PizzaTestDrive {
 public static void main(String[] args) {
  PizzaStore nyStore = new NYPizzaStore();
  PizzaStore chicagoStore = new ChicagoPizzaStore();
  Pizza pizza = nyStore.orderPizza("cheese");
  System.out.println("Ethan ordered a " + pizza.getName() + "\n");
  pizza = chicagoStore.orderPizza("cheese");
  System.out.println("Joel ordered a " + pizza.getName() + "\n");
 }
}

  • 大小: 38.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics