工厂模式和抽象工厂模式之间的基本区别是什么?
当前回答
抽象工厂的示例/场景
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:高级用户体验,黄金用户体验,普通用户体验。
其他回答
这些工厂的主要区别是什么时候你想用工厂做什么,什么时候你想使用它。
有时候,当你在做IOC(控制反转,例如构造函数注入)时,你知道你可以创建固体对象。如上面的水果示例所述,如果准备创建水果对象,可以使用简单的工厂模式。
但是很多时候,你不想创建实体对象,它们会在程序流的后面出现。但是配置告诉你你想在开始时使用什么样的工厂,而不是创建对象,你可以将从公共工厂类派生的工厂传递给IOC中的构造函数。
所以,我认为这也是关于对象的生命周期和创建。
//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设计模式的示例
我认为我们可以通过查看Java8示例代码来理解这两者之间的区别:
interface Something{}
interface OneWhoCanProvideSomething {
Something getSomething();
}
interface OneWhoCanProvideCreatorsOfSomething{
OneWhoCanProvideSomething getCreator();
}
public class AbstractFactoryExample {
public static void main(String[] args) {
//I need something
//Let's create one
Something something = new Something() {};
//Or ask someone (FACTORY pattern)
OneWhoCanProvideSomething oneWhoCanProvideSomethingOfTypeA = () -> null;
OneWhoCanProvideSomething oneWhoCanProvideSomethingOfTypeB = () -> null;
//Or ask someone who knows soemone who can create something (ABSTRACT FACTORY pattern)
OneWhoCanProvideCreatorsOfSomething oneWhoCanProvideCreatorsOfSomething = () -> null;
//Same thing, but you don't need to write you own interfaces
Supplier<Something> supplierOfSomething = () -> null;
Supplier<Supplier<Something>> supplierOfSupplier = () -> null;
}
}
现在的问题是你应该使用哪种创造方式以及为什么: 第一种方式(没有模式,只是普通的构造函数):自己创建不是一个好主意,你必须做所有的工作,你的客户端代码绑定到特定的实现。
第二种方式(使用Factory模式):为您提供了可以传递任何类型的实现的好处,这些实现可以基于某些条件(可能是传递给创建方法的参数)提供不同类型的东西。
第三种方法(使用抽象工厂模式):这为您提供了更多的灵活性。您可以根据某些条件(可能是传递的参数)找到不同类型的创建者。
请注意,您总是可以通过将两个条件组合在一起来摆脱工厂模式(这稍微增加了代码的复杂性和耦合),我想这就是为什么我们很少看到抽象工厂模式的实际用例。
此信息的来源:http://java.dzone.com/news/intro-design-patterns-abstract
抽象工厂与工厂方法
抽象工厂的方法被实现为工厂方法。抽象工厂模式和工厂方法模式都通过抽象类型和工厂将客户端系统与实际实现类解耦。 工厂方法通过继承创建对象,而抽象工厂通过组合创建对象。
抽象工厂模式由AbstractFactory、ConcreteFactory、AbstractProduct、ConcreteProduct和客户端组成。
如何实现
抽象工厂模式可以使用工厂方法模式、原型模式或单例模式来实现。ConcreteFactory对象可以作为一个单例对象实现,因为只需要一个ConcreteFactory对象实例。
工厂方法模式是抽象工厂模式的简化版本。工厂方法模式负责创建属于一个家族的产品,而抽象工厂模式处理多个产品家族。
工厂方法使用接口和抽象类将客户端与生成器类和生成的产品解耦。摘要工厂有一个生成器,它是几个工厂方法的容器,以及将客户端与生成器和产品分离的接口。
何时使用工厂方法模式
当需要将客户端与其使用的特定产品解耦时,请使用工厂方法模式。使用Factory方法可以减轻客户端创建和配置产品实例的责任。
何时使用抽象工厂模式
当客户端必须从产品类中解耦时,使用抽象工厂模式。 特别适用于程序配置和修改。抽象工厂模式还可以强制约束哪些类必须与其他类一起使用。建造新的混凝土工厂可能需要做很多工作。
例子:
抽象工厂实例1
本规范适用于制备不同类型面食的圆盘 在面食机中有一个抽象工厂,每个特定的圆盘都是一个工厂。 所有的工厂(意大利面食机磁盘)从抽象工厂继承他们的属性。 每个单独的磁盘都包含如何制作意大利面,而意大利面机没有。
例子2:
冲压设备对应于抽象工厂,因为它是一个 用于创建抽象产品对象的操作接口。 模具对应混凝土工厂,因为他们创造一个混凝土产品。 每个部件类别(罩、门等)都对应于抽象产品。 具体部件(即99凯美瑞的司机侧门)对应 混凝土制品。
工厂方法示例:
玩具公司对应于创造者,因为它可以使用工厂来创建产品对象。制造特定类型玩具(马或汽车)的玩具公司的部门对应于ConcreteCreator。
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.
推荐文章
- 在PHP5中创建单例设计模式
- 什么时候我们应该使用观察者和可观察对象?
- 在哪里放置AutoMapper.CreateMaps?
- 使用Enum实现单例(Java)
- 由Jon Skeet撰写的《Singleton》澄清
- 为什么c#不提供c++风格的'friend'关键字?
- 什么是包装器类?
- MVC, MVP和MVVM设计模式在编码c#方面有什么不同
- 设计模式:工厂vs工厂方法vs抽象工厂
- 用MVVM处理WPF中的对话框
- 战略设计模式与国家设计模式的区别是什么?
- 什么是反模式?
- 让setter返回"this"是不好的做法吗?
- 在c++中是否有与typedef关键字等价的Java或方法论?
- Android上使用哪些架构模式?