javaoopdesign-patternsfactory-methodtemplate-method-pattern

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


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.


Solution

  • Both Factory method pattern and Template method pattern have similar design but they vary for their purpose.

    Factory method is a creational pattern where object creation is the responsibility of the child class.

    A pattern where a class defines an abstract method for an object creation and another method using the created object and thereby allowing the subclasses to provide the implementation of the creational method is factory method.

    Template method is a behavioral pattern where behaviour is the responsibility of the sub class.

    A pattern where a class defines an abstract method for a behaviour and another method invokes the abstract method to have the behaviour executed, the behaviour is implemented by the subclass. Hence its the parent class invoking the implementation of the child class without having any explicit compile time dependency on its subclass. This is true for the factory method pattern as well. But both differs in their intent.

    In other words we can say Factory method pattern creates object in a similar way as Template method pattern executes the behaviour.