工厂方法模式遵循模板模式,抽象工厂模式不遵循模板模式吗? [英] Does factory method pattern follow template pattern, and abstract factory pattern doesn't?

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

问题描述

在Gamma等人的设计模式"中,工厂方法"具有以下结构:

抽象工厂"具有以下结构:

为什么Creator在工厂方法模式中具有方法AnOperation()来调用FactoryMethod(),而AbstractFactory在抽象工厂模式中却没有方法来调用CreateProductA()CreateProductB()? >

Creator的方法AnOperation()调用FactoryMethod()是否遵循模板模式,其结构为

?

解决方案

Factory方法和Abstract Factory模式都不是(遵循")Template Method设计模式的应用.尽管其解决方案模板的UML类图可能看起来相似,但它们实际上有助于实现不同的目标.

这两个工厂模式涉及允许子类自定义创建类型的对象的方式.有关不同工厂模式之间差异的讨论,请参见这篇文章.

Template Method设计模式与对象的创建无关,而与将变异点引入算法有关.

这就是说,这三种模式的共同点在于,它们将某些行为推迟给子类.请参阅这篇文章,以获取有关您的问题的另一种见解.

In Design Pattern by Gamma et al, "Factory Method" has the following structure:

"Abstract Factory" has the following structure:

Why does Creator has a method AnOperation() to callFactoryMethod() in Factory Method pattern, while AbstractFactory doesn't have a method to call CreateProductA() and CreateProductB() in Abstract Factory pattern?

Does Creator's method AnOperation() callingFactoryMethod() follow the Template pattern, whose structure is

?

解决方案

Neither the Factory method nor the Abstract Factory pattern are applications of ("follow") the Template Method design pattern. Although the UML class diagram of their solution template may look similar, they actually help accomplish different goals.

The two factory patterns are concerned with allowing subclasses to customize how objects of a certain type are created. See this post for a discussion of the differences between the different factory patterns.

The Template Method design pattern is not concerned with the creation of objects, but rather in introducing variation points into an algorithm.

This being said, what all three patterns have in common is that they defer some behavior to subclasses. See this post for an alternative take on your question.

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

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