Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Pattern

 
Raghubir Bose
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Factory Method is nothing but a Template Method that creates objects

a. True
b False

Answer a:

 
s khosa
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. This sounds correct. Both in factory as well as template method:

- base class is abstract. In factory, you intend your child classes to come up with your own implementations, whereas in Template you want child classes to implement some extra functionality(other than what is done in base class)

Only difference is that in Factory you return diff object of child class depending on run time behaviour.

Thanks
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In template method pattern, the skeleton of an algorithm is defined in the superclass. The implementation of some steps are deferred to the subclasses. In other words, it provides a framework where a subclass can redefine an algorithm encapsulated in the superclass without changing the algorithm's structure. The methods which are left abstract in the superclass and implemented in the subclass are called primitive operations. The method that defines the algorithm using the primitive operations is called the template method. A template method / primitive operation can do anything and there are no restrictions on what you can do in them

In the factory method pattern, you define an abstract factory method in the superclass and implement it in the subclass. But when you implement it, you can only create an object in the factory method and return it to the caller and the caller uses the created object in some way . Hence there is a restriction on what you can do in a factory method. Hence factory method pattern is a more specialized form of the template method pattern. Both of them use inheritance to defer implementation to subclasses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic