工厂模式和抽象工厂模式之间的基本区别是什么?
当前回答
工厂方法:您有一个工厂,它创建派生自特定基类的对象
抽象工厂:你有一个创建其他工厂的工厂,这些工厂反过来创建从基类派生的对象。这样做是因为您通常不只是想创建单个对象(与Factory方法一样)—相反,您想创建相关对象的集合。
其他回答
//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提供者、中间件驱动程序等等。
我认为我们可以通过查看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模式):为您提供了可以传递任何类型的实现的好处,这些实现可以基于某些条件(可能是传递给创建方法的参数)提供不同类型的东西。
第三种方法(使用抽象工厂模式):这为您提供了更多的灵活性。您可以根据某些条件(可能是传递的参数)找到不同类型的创建者。
请注意,您总是可以通过将两个条件组合在一起来摆脱工厂模式(这稍微增加了代码的复杂性和耦合),我想这就是为什么我们很少看到抽象工厂模式的实际用例。
基本的区别:
工厂:创建对象时不向客户端公开实例化逻辑。
工厂方法:定义用于创建对象的接口,但让子类决定实例化哪个类。Factory方法允许类延迟实例化到子类
抽象工厂:提供一个接口,用于创建一系列相关或依赖的对象,而无需指定它们的具体类。
AbstractFactory模式使用组合将创建对象的责任委托给另一个类,而Factory方法模式使用继承并依赖于派生类或子类来创建对象
来自oodesign的文章:
工厂类图:
例如:StaticFactory
public class ShapeFactory {
//use getShape method to get object of type shape
public static Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
} else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
}
非静态工厂实现FactoryMethod的例子在这篇文章中可用:
设计模式:工厂vs工厂方法vs抽象工厂
何时使用:客户端只需要一个类,并不关心它得到的是哪个具体实现。
工厂方法类digaram:
何时使用:客户端不知道在运行时需要创建什么具体的类,而只是想获得一个可以完成这项工作的类。
来自dzone的抽象工厂类图
何时使用:当您的系统必须创建多个产品系列,或者您希望提供一个产品库而不公开实现细节时。
以上文章中的源代码示例非常有助于清楚地理解这些概念。
相关SE问题与代码示例:
工厂模式。什么时候使用工厂方法?
差异:
抽象工厂类通常使用工厂方法来实现,但它们也可以使用原型来实现 设计开始使用工厂方法(不太复杂,更可定制,子类激增),然后向其他需要更多灵活性的创建模式(更灵活,更复杂)发展。 工厂方法通常在模板方法中调用。
其他有用的文章:
来自sourcemmaking的Factory_method
摘要工厂从sourcemmaking
来自journaldev的抽象工厂设计模式