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
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.
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.
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.
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.
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.
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.
To address the comment above:
These patterns are not used exclusively, e.g., Strategy can use Template Method, Factory Method, or other patterns.
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With