Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 656
nits.kk Avatar asked Apr 01 '19 18:04

nits.kk


People also ask

What type of pattern is the factory pattern?

The factory method is a creational design pattern, i.e., related to object creation. In the Factory pattern, we create objects without exposing the creation logic to the client and the client uses the same common interface to create a new type of object.

Is Factory Method a structural design pattern?

Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

Is a factory or a template to create new objects of same type?

Factory Method is to creating objects as Template Method is to implementing an algorithm. A superclass specifies all standard and generic behavior (using pure virtual "placeholders" for creation steps), and then delegates the creation details to subclasses that are supplied by the client.

What is template method pattern in design pattern?

Template method design pattern is to define an algorithm as a skeleton of operations and leave the details to be implemented by the child classes. The overall structure and sequence of the algorithm are preserved by the parent class. Template means Preset format like HTML templates which has a fixed preset format.


1 Answers

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!

like image 172
Rafael Avatar answered Oct 19 '22 11:10

Rafael