Java 设计模式-抽象工厂模式

485 阅读1分钟
  1. 需求分析:
    工厂方法中我们定义了一个比萨店来规范比萨的制作. 现在为了进一步规范, 我们希望确保加盟店都能够使用高质量的原料, 所以我们需要构建一个原料工厂. 由于比萨的制作需要多种原料, 我们希望能够调用原料工厂的不同方法来获取不同的原料.

    public interface PizzaIngredientFactory {
        // 获取面团
        public Dough createDough();
        // 获取酱
        public Sauce createSauce();
        // 获取奶酪
        public Cheese createCheese();
        // 获取蔬菜
        public Veggies[] createVeggies();
        // 获取意大利肠
        public Pepperoni createPepperoni();
        // 获取蛤蜊
        public Clam createClam();
    }
    
    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 {new Garlic(), new Onion(), new Mushroom(), new RedPepper()};
            return veggies;
        }
        
        public Clam createClam() {
            return new FreshClam();
        }
    }
    
    public abstract class Pizza {
        private String name;
        private Dough dough;
        private Sauce sauce;
        private Veggies veggies[];
        private Cheese cheese;
        private Pepperoni pepperoni;
        private Clam clam;
        
        protected 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;
        }
        
        public String getName() {
            return this.name;
        }
    }
    
    public class CheesePizza extends Pizza {
        private PizzaIngredientFactory ingredientFactory;
        
        public CheesePizza(PizzaIngredientFacotry ingredientFactory) {
            this.ingredientFactory = ingredientFactory;
        }
        
        @Override
        public void prepare() {
            // 这里调用原料工厂
            dough = ingredientFactory.createDough();
            sauce = ingredientFactory.createSauce();
            cheese = ingredientFactory.createCheese();
        }
    }
    
  2. 抽象工厂定义:
    Provide an interface for creating families of related or dependent objects without specifying their concrete classes(为创建一组相关或相互依赖的对象提供一个接口, 而且无需指定它们的具体类).

    abstract factory

  3. 抽象工厂的应用:
    javax.xml.parsers.DocumentBuilderFactory#newInstance() 方法
    javax.xml.transform.TransformerFactory#newInstance() 方法 javax.xml.xpath.XPathFactory#newInstance() 方法

  4. 参考:
    [1] : Java 库中的设计模式
    [2] : 设计模式之禅
    [3] : Head First 设计模式
    [4] : 三种工厂方法模式