工厂模式和抽象工厂模式之间的基本区别是什么?
当前回答
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.
其他回答
此信息的来源:http://java.dzone.com/news/intro-design-patterns-abstract
抽象工厂与工厂方法
抽象工厂的方法被实现为工厂方法。抽象工厂模式和工厂方法模式都通过抽象类型和工厂将客户端系统与实际实现类解耦。 工厂方法通过继承创建对象,而抽象工厂通过组合创建对象。
抽象工厂模式由AbstractFactory、ConcreteFactory、AbstractProduct、ConcreteProduct和客户端组成。
如何实现
抽象工厂模式可以使用工厂方法模式、原型模式或单例模式来实现。ConcreteFactory对象可以作为一个单例对象实现,因为只需要一个ConcreteFactory对象实例。
工厂方法模式是抽象工厂模式的简化版本。工厂方法模式负责创建属于一个家族的产品,而抽象工厂模式处理多个产品家族。
工厂方法使用接口和抽象类将客户端与生成器类和生成的产品解耦。摘要工厂有一个生成器,它是几个工厂方法的容器,以及将客户端与生成器和产品分离的接口。
何时使用工厂方法模式
当需要将客户端与其使用的特定产品解耦时,请使用工厂方法模式。使用Factory方法可以减轻客户端创建和配置产品实例的责任。
何时使用抽象工厂模式
当客户端必须从产品类中解耦时,使用抽象工厂模式。 特别适用于程序配置和修改。抽象工厂模式还可以强制约束哪些类必须与其他类一起使用。建造新的混凝土工厂可能需要做很多工作。
例子:
抽象工厂实例1
本规范适用于制备不同类型面食的圆盘 在面食机中有一个抽象工厂,每个特定的圆盘都是一个工厂。 所有的工厂(意大利面食机磁盘)从抽象工厂继承他们的属性。 每个单独的磁盘都包含如何制作意大利面,而意大利面机没有。
例子2:
冲压设备对应于抽象工厂,因为它是一个 用于创建抽象产品对象的操作接口。 模具对应混凝土工厂,因为他们创造一个混凝土产品。 每个部件类别(罩、门等)都对应于抽象产品。 具体部件(即99凯美瑞的司机侧门)对应 混凝土制品。
工厂方法示例:
玩具公司对应于创造者,因为它可以使用工厂来创建产品对象。制造特定类型玩具(马或汽车)的玩具公司的部门对应于ConcreteCreator。
使用Factory模式,您可以生成特定接口(例如,IFruit)的实现实例(Apple、Banana、Cherry等)。
使用抽象工厂模式,您可以为任何人提供他们自己的工厂提供一种方法。这使得您的仓库可以是IFruitFactory或IJuiceFactory,而不需要您的仓库了解任何关于水果或果汁的信息。
工厂模式: 工厂生产iproduct实现
工厂模式: 一个工厂-工厂生产IFactories, IFactories反过来生产IProducts:)
[根据评论更新] 我之前写的至少在维基百科上是不正确的。抽象工厂就是一个简单的工厂接口。有了它,您可以在运行时切换工厂,以允许在不同的上下文中使用不同的工厂。例如针对不同操作系统的不同工厂、SQL提供者、中间件驱动程序等等。
延伸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.
}
}
抽象工厂的示例/场景
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:高级用户体验,黄金用户体验,普通用户体验。