工厂方法与抽象工厂 [英] Factory Method Vs Abstract Factory

查看:104
本文介绍了工厂方法与抽象工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关Factory方法的信息,其中子类创建了所需的对象,而Abstract Factory有一些方法,其中的具体类创建了所需的对象

I have read about Factory Method where Sub class creates needed Object and Abstract Factory has methods where concrete classes creates needed Object

Factory Method

Factory Method

public class PizzaStore {

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

    abstract Pizza createPizza(String type);

}

public NewYorkPizzaStore extends PizzaStore {

    public Pizza createPizza(String type) {
        Pizza pizza = null;
        if("cheese".equals(type)) {
            pizza = new CheesePizza();
        }
        else if("onion".equals(type)) {
            pizza = new OnionPizza();
        }

        return pizza;
    }

}

public class pizzaTestDriveByFactoryMethod() {

    public static void main(String args[]) {
        PizzaStore ps =  new NewYorkPizzaStore();
        ps.orderPizza("cheese");
    }

}

使用工厂

public class NewYorkPizzaFactory extends PizzaFactory {

    public Pizza createPizza(String pizza) {
        Pizza pizza = null;
        if("cheese".equals(type)) {
            pizza = new CheesePizza();
        } else if("onion".equals(type)) {
            pizza = new OnionPizza();
        }

        return pizza;
    }

}

public class PizzaStore {

    PizzaFactory factory;

    public PizzaStore(PizzaFactory factory) {
        this.factory =  factory
    }

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

}

public class pizzaTestDriveByAbstractFactory() {

    public static void main(String args[]) {
        PizzaFactory nwFactory = new NewYorkPizzaFactory();
        PizzaStore ps =  new PizzaStore(nwFactory);
        ps.orderPizza("cheese");
    }

}

使用工厂方法实现的相同用例和抽象工厂。为什么应该使用FactoryMethod而不是使用Abstract Factory或Utility Factory(例如Chicago Factory / NewYorkFactory)。在哪种情况下,工厂方法对抽象方法有用?

Same use case implemented using Factory Method and Abstract Factory. Why there should be a FactoryMethod instead of using Abstract Factory or a Utility Factory (Such as Chicago Factory/NewYorkFactory). In which case Factory method is useful on Abstract Method?

推荐答案

您应该问工厂任务在概念上是否与基类相关联和/或子类(例如createPizza)或不(例如procurePizzaBoxes)。披萨盒供应商不仅在概念上有所区别,而且可以互换。甚至可能是一家为每个城市提供比萨盒的全国性公司。

You should ask whether the factory task is conceptually tied to the base class and/or subclasses (like createPizza) or not (like procurePizzaBoxes). The pizza box supplier is not only conceptually distinct, but can be swapped. It might even be one national company supplying pizza boxes to every city.

另一种确定的方法是,如果要创建子类只是为了实现工厂方法,则您最好将其分解为抽象工厂。

Another way to do decide is that if you're making subclasses just to implement the factory method, then you'd be better off factoring it out into an abstract factory.

但是,如果子类具有自己的特征并且无论是否需要存在,并且该方法的实现都与它们绑定在一起(即使它是procurePizzaBoxes,但是供应商是本地的)并且不是重要的细节),那么您应该使用工厂方法作为多态性和OO的自然应用,并减少类的数量。

But if the subclasses have their own characteristics and need to exist regardless, and the implementation of the method is tied to them (even if it's procurePizzaBoxes, but the supplier is local and isn't an important detail), then you should use factory method as a natural application of polymorphism and OO, and to keep the class count down.

从工厂重构抽象工厂的方法可能比反向方法更容易,并且需要更少的类,因此可以认为是比较保守的选择。

Refactoring from factory method to abstract factory is probably easier than the reverse, and requires fewer classes, so can be considered the more conservative choice.

这篇关于工厂方法与抽象工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆