Factory方法模式是Template方法模式的特例吗? [英] Is Factory method pattern a specialized case of Template method pattern?

查看:121
本文介绍了Factory方法模式是Template方法模式的特例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GOF讨论工厂方法"模式的框架.框架需要对象,但是对象的实现取决于应用程序,因此创建了创建对象的抽象方法.同样,由于需要返回类型,因此定义了所需对象的接口,它定义了对象所需的api.实际对象由子类创建(具体应用程序).这是一种创造模式.

GOF talks about frameworks for "Factory method" pattern. Frameworks need objects but implementation of objects depends upon application hence an abstract method to create the object is created. Also as return type is needed so interface for the object needed is defined, it defines the apis needed for the object. Actual objects are created by subclasses (concrete Application) . This is a creational pattern.

对于Template模式,唯一的变化是封装类不知道某些行为的实现,因此将其抽象为一个方法,使用它,但将实现留给子类.这是行为模式.

For Template pattern the only change is that the encapsulating class does not know the implementation of certain behavior hence it abstracts it in a method , uses it but leaves the implementation to the subclasses. This is behavioral pattern.

两者之间唯一的区别是

1. Factory method is creational and Template is behavioural.
2. Factory method abstracts a method to create an object where as template pattern abstracts a method for some policy or algorithm. 

示例代码

 /**factory-method example**/
 public abstract class Application{          
        public void create(){
              View contentView = createContentView();
              Menu menu = contentView.obtainMenu();
              generateMenuItems(menu);
        }
        public abstract View createContentView(); //factory-method
        public void generateMenuItems(Menu menu){
              // some code
        }
 }

  /** Product Specification**/            
 public interface View{
      public abstract Menu obtainMenu();
      // other abstract method of product
 }

现在,使用上述代码的用户代码将子类化Application并为createContentView()提供实现.

Now User code using above will subclass Application and provide implementation for createContentView().

模板方法的基本特征:调用其抽象方法的父类具体方法.

Template method basic characterstic : Parent class concrete method invoking its abstract method.

Factory方法:通过其子类实现产品创建.

Factory method : lets the product creation be implemented by its sub classes.

以上示例均适用于两者.实际上,任何适用于Factory方法的示例也适用于Template方法.

Above example fits for both. In fact any example for Factory methods fits for Template method as well.

所以很好说

  1. 工厂方法模式是一种专门的模板方法模式,用于获取其实现取决于用户代码的对象,该用户代码可以在子类中提供对象创建的实现
  2. 用于创建对象的模板模式是Factory方法模式.
  1. Factory method pattern is specialized template method pattern for obtaining the object whose implementation is dependent upon user code which can provide implementation of object creation in the subclass
  2. Template pattern if used for object creation is Factory method pattern.

我的第二个疑问:工厂方法(基于基于无性的GOF)是否必须从其他具体方法中调用其抽象产品生产方法?

My second doubt : Is it mandatory for Factory method (which is as per GOF based on inberitence) to invoke its abstract product producing method from its other concrete method ?

如果以上回答为否",则意味着将有一些消费者代码,其代码类型为Factory(组成),将调用factory方法获取产品对象并获得具体的工厂类注射.但是现在这变成了抽象工厂.

If answer to above is 'No' then this means there will be some consumer code which will have an instance of type Factory (composition),will invoke the factory method to obtain the object of a product and will get concrete factory class injected. But now this becomes abstract factory.

推荐答案

我不想过分简化这些模式,但是,是的,两个抽象都完全延迟了实现细节.将客户端与实现细节分离开来会更加灵活,并允许每个客户端独立发展.这是依赖倒置"原理的本质.

I don't want to oversimplify these patterns, but, yes, both abstractions defer implementation details completely. Decoupling clients from implementation details is more flexible and allows each to evolve independently; this is the essence of the Dependency Inversion Principle.

  • 工厂方法完全延迟创建
  • 策略完全阻止行为
  • Factory Method defers creation completely
  • Strategy defers behavior completely

要解决以上评论:

  • 模板方法部分延迟行为(emem)(不太完全一样)
  • Template Method defers behavior partially (not quite the same)

这些模式不是专门使用的,例如,策略可以使用模板方法,工厂方法或其他模式.

These patterns are not used exclusively, e.g., Strategy can use Template Method, Factory Method, or other patterns.

我希望这会有所帮助!

这篇关于Factory方法模式是Template方法模式的特例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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