抽象工厂模式: 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要明确指定具体类.
全部代码: http://download.csdn.net/detail/u012515223/7403553
具体方法:
1. 提供一个抽象工厂(abstract factory)接口(interface)类, 不同的具体工厂(concrete factory)继承此类.
代码:
/** * @time 2014年5月26日 */ package factory; /** * @author C.L.Wang * */ public interface PizzaIngredientFactory { public Dough createDough(); public Sauce createSauce(); public Cheese createCheese(); public Veggies[] createVeggies(); public Pepperoni createPepperoni(); public Clams createClam(); }
2. 具体工厂(concrete factory), 实现抽象工厂(abstract factory)接口(interface).
代码:
/** * @time 2014年5月26日 */ package factory; /** * @author C.L.Wang * */ public class NYPizzaIngredientFactory implements PizzaIngredientFactory { /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createDough() */ @Override public Dough createDough() { // TODO Auto-generated method stub return new ThinCrustDough(); } /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createSauce() */ @Override public Sauce createSauce() { // TODO Auto-generated method stub return new MarinaraSauce(); } /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createCheese() */ @Override public Cheese createCheese() { // TODO Auto-generated method stub return new ReggianoCheese(); } /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createVeggies() */ @Override public Veggies[] createVeggies() { // TODO Auto-generated method stub Veggies veggies[] = {new Garlic(), new Onion(), new Mushroom(), new RedPepper()}; return veggies; } /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createPepperoni() */ @Override public Pepperoni createPepperoni() { // TODO Auto-generated method stub return new SlicedPepperoni(); } /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createClam() */ @Override public Clams createClam() { // TODO Auto-generated method stub return new FreshClams(); } } /** * @time 2014年5月26日 */ package factory; /** * @author C.L.Wang * */ public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory { /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createDough() */ @Override public Dough createDough() { // TODO Auto-generated method stub return new ThickCrustDough(); } /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createSauce() */ @Override public Sauce createSauce() { // TODO Auto-generated method stub return new PlumTomatoSauce(); } /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createCheese() */ @Override public Cheese createCheese() { // TODO Auto-generated method stub return new MozzarellaCheese(); } /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createVeggies() */ @Override public Veggies[] createVeggies() { // TODO Auto-generated method stub Veggies veggies[] = {new BlackOlives(), new Spinach(), new Eggplant()}; return veggies; } /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createPepperoni() */ @Override public Pepperoni createPepperoni() { // TODO Auto-generated method stub return new SlicedPepperoni(); } /* (non-Javadoc) * @see factory.PizzaIngredientFactory#createClam() */ @Override public Clams createClam() { // TODO Auto-generated method stub return new FrozenClams(); } }
3. 产品抽象(abstract)父类, 提供接口, 供具体产品(concrete product)调用.
代码:
/** * @time 2014年5月26日 */ package factory; /** * @author C.L.Wang * */ 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("Bake for 25 minutes at 350"); } void cut() { System.out.println("Cutting the pizza into diagonal slices"); } void box() { System.out.println("Place pizza in official PizzaStore box"); } void setName(String name) { this.name = name; } public String getName() { return name; } }
4. 具体产品(concrete product)继承产品抽象(abstract)父类, 使用工厂类实现继承接口, 通过不同的工厂生产不同的产品;
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索setup factory
, new
, return
, javadoc
, override
, public
, non monotonous
, non parseable
, method
, Factory
Factory模式
abstract抽象类、abstract factory、abstract factory模式、factory pattern、logback pattern 详解,以便于您获取更多的相关知识。