工厂模式和抽象工厂模式之间的基本区别是什么?


当前回答

很多人可能会感到惊讶,但这个问题是不正确的。如果你在面试中听到这个问题,你需要帮助面试官理解你的困惑在哪里。

让我们从一个事实开始,即没有具体的模式被称为“工厂”。有一种模式叫做“抽象工厂”,也有一种模式叫做“工厂方法”。

那么,“工厂”是什么意思呢?以下之一(根据引用的范围,都可以认为是正确的):

有些人用它作为“抽象工厂”的别名(快捷方式)。 有些人使用它作为“工厂方法”的别名(快捷方式)。 有些人将其用作所有工厂/创建模式的通用名称。例如,“抽象工厂”和“工厂方法”都是工厂。

不幸的是,许多人使用“Factory”来表示另一种类型的工厂,它创建了一个或多个工厂(或它们的接口)。根据他们的理论:

Product实现了IProduct,它是由Factory创建的 实现了由AbstractFactory创建的IFactory。

为了理解这有多愚蠢,让我们继续我们的方程:

AbstractFactory实现了IAbstractFactory,它由… AbstractAbstractFactory ? ?

我希望你能明白我的意思。不要困惑,请不要发明那些无缘无故存在的东西。

-

附注:产品的工厂是AbstractFactory,而抽象工厂的工厂也是AbstractFactory的另一个例子。

其他回答

抽象工厂是创建相关对象的接口,而工厂方法是一种方法。抽象工厂采用工厂方法实现。

Abstract Factory is template for creating different type of interfaces. Suppose you have project that requires you to parse different types of csv files containing quantity, price and item specific information like some contain data about fruits other about chocolates and then after parsing you need to update this information in their corresponding database so now you can have one abstract factory returning you parser and modifier factory and then this parser factory can return you Chocolate parser object,Fruit Parser Object etc. and similarly Modifier Factory can return Chocolate modifier object , Fruit Modifier object etc.

工厂方法和抽象工厂都使客户端与具体类型解耦。两者都创建对象,但是工厂方法使用继承,而抽象工厂方法使用组合。

工厂方法在子类中继承,用于创建具体的对象(产品),而抽象工厂提供了用于创建相关产品族的接口,这些接口的子类定义了如何创建相关产品。

然后这些子类在实例化后被传递到产品类中,在产品类中它被用作抽象类型。抽象工厂中的相关产品通常使用工厂方法来实现。

延伸John Feminella的回答:

Apple, Banana, Cherry实现了FruitFactory,它有一个叫做Create的方法,它只负责创建Apple, Banana或Cherry。Factory方法就完成了。

现在,你想用你的水果创建一个特殊的沙拉,这就是你的抽象工厂。抽象工厂知道如何用苹果、香蕉和樱桃制作你的特殊沙拉。

public class Apple implements Fruit, FruitFactory {
    public Fruit Create() {
        // Apple creation logic goes here
    }
}

public class Banana implements Fruit, FruitFactory {
    public Fruit Create() {
        // Banana creation logic goes here
    }
}

public class Cherry implements Fruit, FruitFactory {
    public Fruit Create() {
        // Cherry creation logic goes here
    }
}

public class SpecialSalad implements Salad, SaladFactory {
    public static Salad Create(FruitFactory[] fruits) {
        // loop through the factory and create the fruits.
        // then you're ready to cut and slice your fruits 
        // to create your special salad.
    }
}

抽象工厂模式

Provide an interface for creating families of related or dependent objects without specifying their concrete classes. The Abstract Factory pattern is very similar to the Factory Method pattern. One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation. Actually, the delegated object frequently uses factory methods to perform the instantiation!

工厂模式

工厂模式就是这样的例子 创建型模式 创建模式抽象了 对象实例化过程。他们 隐藏对象是如何创建和帮助的 使整个系统相互独立 它的对象是如何创建的 组成。 类创建模式的重点 使用继承来决定 对象进行实例化 工厂方法 对象创建模式所关注的 实例化的委托 到另一个对象 抽象工厂

参考: 工厂vs抽象工厂