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


当前回答

抽象工厂的示例/场景

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:高级用户体验,黄金用户体验,普通用户体验。

其他回答

点击这里查看:http://www.allapplabs.com/java_design_patterns/abstract_factory_pattern.htm 似乎Factory方法使用一个特定的类(不是抽象类)作为基类,而抽象工厂则使用一个抽象类。此外,如果使用接口而不是抽象类,结果将是抽象工厂模式的不同实现。

:D

工厂方法:您有一个工厂,它创建派生自特定基类的对象

抽象工厂:你有一个创建其他工厂的工厂,这些工厂反过来创建从基类派生的对象。这样做是因为您通常不只是想创建单个对象(与Factory方法一样)—相反,您想创建相关对象的集合。

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

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

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

抽象工厂的示例/场景

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:高级用户体验,黄金用户体验,普通用户体验。

此信息的来源:http://java.dzone.com/news/intro-design-patterns-abstract

抽象工厂与工厂方法

抽象工厂的方法被实现为工厂方法。抽象工厂模式和工厂方法模式都通过抽象类型和工厂将客户端系统与实际实现类解耦。 工厂方法通过继承创建对象,而抽象工厂通过组合创建对象。

抽象工厂模式由AbstractFactory、ConcreteFactory、AbstractProduct、ConcreteProduct和客户端组成。

如何实现

抽象工厂模式可以使用工厂方法模式、原型模式或单例模式来实现。ConcreteFactory对象可以作为一个单例对象实现,因为只需要一个ConcreteFactory对象实例。

工厂方法模式是抽象工厂模式的简化版本。工厂方法模式负责创建属于一个家族的产品,而抽象工厂模式处理多个产品家族。

工厂方法使用接口和抽象类将客户端与生成器类和生成的产品解耦。摘要工厂有一个生成器,它是几个工厂方法的容器,以及将客户端与生成器和产品分离的接口。

何时使用工厂方法模式

当需要将客户端与其使用的特定产品解耦时,请使用工厂方法模式。使用Factory方法可以减轻客户端创建和配置产品实例的责任。

何时使用抽象工厂模式

当客户端必须从产品类中解耦时,使用抽象工厂模式。 特别适用于程序配置和修改。抽象工厂模式还可以强制约束哪些类必须与其他类一起使用。建造新的混凝土工厂可能需要做很多工作。

例子:

抽象工厂实例1

本规范适用于制备不同类型面食的圆盘 在面食机中有一个抽象工厂,每个特定的圆盘都是一个工厂。 所有的工厂(意大利面食机磁盘)从抽象工厂继承他们的属性。 每个单独的磁盘都包含如何制作意大利面,而意大利面机没有。

例子2:

冲压设备对应于抽象工厂,因为它是一个 用于创建抽象产品对象的操作接口。 模具对应混凝土工厂,因为他们创造一个混凝土产品。 每个部件类别(罩、门等)都对应于抽象产品。 具体部件(即99凯美瑞的司机侧门)对应 混凝土制品。

工厂方法示例:

玩具公司对应于创造者,因为它可以使用工厂来创建产品对象。制造特定类型玩具(马或汽车)的玩具公司的部门对应于ConcreteCreator。