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


当前回答

抽象工厂的示例/场景

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

我的资源是:StackOverflow, tutorialspoint.com, programmers.stackexchange.com和CodeProject.com。


工厂方法(也称为工厂)用于解耦接口实现的客户端。例如,我们有一个具有圆和方两个实现的Shape接口。我们已经定义了一个工厂类,它带有一个工厂方法和一个确定参数,如Type和Shape接口的新的相关实现。


抽象工厂包含多个工厂方法或多个工厂实现的工厂接口。 对于上面的下一个示例,我们有一个带有两个红色和黄色实现的颜色接口。 我们已经用两个RedCircleFactory和YellowSquareFactory定义了一个ShapeColorFactory接口。下面的代码解释这个概念:

interface ShapeColorFactory
{
    public Shape getShape();
    public Color getColor();
}

class RedCircleFactory implements ShapeColorFactory
{
    @Override
    public Shape getShape() {
        return new Circle();
    }

    @Override
    public Color getColor() {
        return new Red();
    }
}
class YellowSquareFactory implements ShapeColorFactory
{
    @Override
    public Shape getShape() {
        return new Square();
    }

    @Override
    public Color getColor() {
        return new Yellow();
    }
} 

这里是FactoryMethod和AbstractFactory的区别。工厂方法返回接口的具体类,而抽象工厂返回工厂的工厂。换句话说,抽象工厂返回不同组合的一系列界面。


我希望我的解释有用。

抽象工厂的示例/场景

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

//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设计模式的示例

对于John的回答,我有几点要补充如下:

抽象工厂是工厂中的工厂!

使用“Factory方法”(因为只有“Factory”是不明确的),您可以生成特定接口的实现(Lemon、Orange等)——比如,IFruit。这个工厂可以被称为CitricFruitFactory。

但是现在您想要创建CitricFruitFactory无法创建的另一种水果。如果您在CitricFruitFactory中创建一个草莓,那么它的代码可能就没有意义了(草莓不是柠檬酸水果!)。

所以你可以创建一个名为RedFruitFactory的新工厂,生产草莓,覆盆子等。

就像John Feminella说的: 使用抽象工厂模式,您可以生成特定工厂接口的实现——例如,IFruitFactory。他们每个人都知道如何创造不同种类的水果。”

IFruitFactory的实现是CitricFruitFactory和RedFruitFactory!