Is Factory method pattern a specialized case of Template method pattern?

给你一囗甜甜゛ 提交于 2019-12-13 03:38:52

问题


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.

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.

Is the only differences between the two are

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. 

example code

 /**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
 }

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 method : lets the product creation be implemented by its sub classes.

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

So it is good to say

  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.

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 ?

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.


回答1:


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

To address the comment above:

  • 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.

I hope this helps!



来源:https://stackoverflow.com/questions/55461586/is-factory-method-pattern-a-specialized-case-of-template-method-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!