设计模式:抽象工厂与工厂方法 [英] Design Patterns: Abstract Factory vs Factory Method

查看:238
本文介绍了设计模式:抽象工厂与工厂方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经阅读了关于抽象工厂vs的其他stackoverflow线程工厂方法。我明白每个模式的意图。但是,我不清楚定义。


Factory Method定义了一个界面
来创建一个对象,但是让
子类决定哪些到
实例化。一个工厂方法让
类将实例化推迟到
子类。



相比之下,抽象工厂
提供了一个创建$ b的接口

$ b $ p

$ p $ - $ a

$ p $ - http://stackoverflow.com/questions/1001767/design-patterns-factory-vs-abstract-factory\">约翰·芬马利亚



>抽象工厂看起来与工厂方法非常相似。我已经画了几个UML类来说明我的观点。



注意:




  • 图表来自www.yuml。 com ,所以他们不是完美的导向。但是它的免费服务:)。

  • 图可能不完美。我还在学习 GoF 设计模式。



工厂方法 / p>



抽象工厂(只有1个成员):





抽象工厂(更多成员): / p>



问题:


  1. 如果抽象工厂只有一个创造者和一个产品,仍然是抽象工厂模式?

  2. 可以从界面创建具体创建者的工厂方法,还是必须从一类?

  3. 如果抽象工厂只能有一个创建者和一个产品,则抽象工厂工厂方法,前者的创建者是一个Interface,后者的创建者是Class?


解决方案

希望这有帮助。它描述了各种类型的工厂。我使用头端设计模式作为参考。我使用 yuml.me 来图表。



静态工厂



是一个带有静态方法的类,用于产品各种子类型的产品。





简单工厂



是可以生成各种子类型的产品的类。 (它比Static Factory更好,当添加新类型时,基本的Product类不需要更改,只需要简单工厂类)





工厂方法



包含一种方法来生成与其类型相关的一种类型的产品。 (它比简单工厂好,因为类型被推迟到子类。)





抽象工厂



<产生一系列有关的类型。它与Factory Method有明显的区别,因为它具有多种类型的方法。 (这是很复杂的,参考下图,以获得更好的现实生活例子)。





.NET Framework中的示例



DbFactoriesProvider是一个简单的工厂,因为它没有子类型。 DbFactoryProvider是一个抽象工厂,因为它可以创建各种相关的数据库对象,如连接和命令对象。




Note: Questions are at the end of the post.

I have read the other stackoverflow threads regarding Abstract Factory vs Factory Method. I understand the intent of each pattern. However, I am not clear on the definition.

Factory Method defines an interface for creating an object, but lets subclasses decide which of those to instantiate. A factory method lets classes defer instantiation to subclasses.

By contrast, an Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.

-John Feminella

The Abstract Factory looks very similar to the Factory Method. I have drawn a few UML classes to illustrate my point.

Note:

  • The diagram are from www.yuml.com so they are not perfectly oriented. But its a free service :).
  • The diagrams may not be perfect. I am still learning the GoF design patterns.

Factory Method:

Abstract Factory (only 1 member):

Abstract Factory (more members):

Questions:

  1. If the Abstract Factory has only one creator and one product, is it still the Abstract Factory pattern? (an interface for creating familes)
  2. Can the Factory Method concrete creator be created from an Interface or does it have to be from a class? (classes defer instantiations to subclasses)
  3. If the Abstract Factory can have only one creator and one product, is the only difference between the Abstract Factory and the Factory Method that the creator for the former is an Interface and the creator for the latter is a Class?

解决方案

Hope this helps. It describes the various types of factories. I used Head First Design Patterns as my reference. I used yuml.me to diagram.

Static Factory

Is a class with a Static Method to product various sub types of Product.

Simple Factory

Is a class that can produce various sub types of Product. (It is better than the Static Factory. When new types are added the base Product class does not need to be changed only the Simple Factory Class)

Factory Method

Contains one method to produce one type of product related to its type. (It is better than a Simple Factory because the type is deferred to a sub-class.)

Abstract Factory

Produces a Family of Types that are related. It is noticeably different than a Factory Method as it has more than one method of types it produces. (This is complicated refer to next diagram for better real-life example).

Example From The .NET Framework

DbFactoriesProvider is a Simple Factory as it has no sub-types. The DbFactoryProvider is an abstract factory as it can create various related database objects such as connection and command objects.

这篇关于设计模式:抽象工厂与工厂方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆