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


当前回答

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.

其他回答

//Abstract factory - Provides interface to create factory of related products
interface PizzaIngredientsFactory{    
   public Dough createDough(); //Will return you family of Dough
   public Clam createClam();   //Will return you family of Clam
   public Sauce createSauce(); //Will return you family of Sauce
}

class NYPizzaIngredientsFactory implements PizzaIngredientsFactory{

   @Override
   public Dough createDough(){
      //create the concrete dough instance that NY uses
      return doughInstance;
   }

   //override other methods
} 

课本上的定义已经由其他答案提供了。我想我也会提供一个例子。

因此这里pizzaingredient factory是一个抽象工厂,因为它提供了创建一系列相关产品的方法。

注意,抽象工厂中的每个方法本身都是一个工厂方法。就像createDough()本身是一个工厂方法,其具体实现将由nypizzafactorentsfactory等子类提供。因此,使用这个方法,每个不同的位置都可以创建属于其位置的具体成分实例。

工厂方法

提供具体实现的实例

在这个例子中: - createDough() -提供面团的具体实现。这是一个工厂方法

抽象工厂

提供创建相关对象族的接口

在这个例子中: pizzafactorentsfactory是一个抽象工厂,因为它允许创建一组相关的对象,如面团,蛤蜊,酱汁。为了创建每个对象族,它提供了一个工厂方法。

来自Head First设计模式的示例

抽象工厂的示例/场景

I live in a place where it rains in the rainy season, snows in winter and hot and sunny in summers. I need different kind of clothes to protect myself from the elements. To do so I go to the store near my house and ask for clothing/items to protect myself. The store keeper gives me the appropriate item as per the environment and depth of my pocket. The items he gives me are of same level of quality and price range. Since he is aware of my standards its easy for him to do so. But when a rich guy from across the street comes up with the same requirements he gets an expensive, branded item. One noticeable thing is all the items he gives to me complement each other in term quality, standard and cost. One can say they go with each other. Same is the case with the items this rich guy gets.

所以通过上面的场景,我现在很欣赏店主的效率。我可以用抽象商店代替这个店主。我们得到的东西是抽象的东西,而我和富人是潜在的客户。我们所需要的只是符合我们需要的产品。

Now I can easily see myself considering an online store which provides a set of services to its numerous clients. Each client belongs to one of the three groups. When a premium group user opens up the site he gets great UI, highly customised advertisement pane, more options in the menus etc. These same set of features are presented to gold user but the functionality in the menu is less, advertisements are mostly relevent, and slightly less egronomic UI. Last is my kind of user, a ‘free group’ user. I am just served enough so that I do not get offended. The UI is a bare minimum, advertisements are way off track so much so that I do not know what comes in it, lastly the menu has only log out.

如果我有机会建立一个像这样的网站,我肯定会考虑抽象工厂模式。

产品:广告面板,菜单,用户界面画师。 摘要工厂:网络商店用户体验 Concreate Factory:高级用户体验,黄金用户体验,普通用户体验。

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

工厂模式: 工厂生产iproduct实现

工厂模式: 一个工厂-工厂生产IFactories, IFactories反过来生产IProducts:)

[根据评论更新] 我之前写的至少在维基百科上是不正确的。抽象工厂就是一个简单的工厂接口。有了它,您可以在运行时切换工厂,以允许在不同的上下文中使用不同的工厂。例如针对不同操作系统的不同工厂、SQL提供者、中间件驱动程序等等。

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

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

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

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

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

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

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

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

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

-

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