File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes Question on Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "Question on Pattern" Watch "Question on Pattern" New topic

Question on Pattern

Raghubir Bose
Ranch Hand

Joined: Feb 18, 2006
Posts: 88
Factory Method is nothing but a Template Method that creates objects

a. True
b False

Answer a:

I will meet the fairy queen soon !
s khosa
Ranch Hand

Joined: May 15, 2003
Posts: 72
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.

Ranch Hand

Joined: Aug 18, 2005
Posts: 372
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.
I agree. Here's the link:
subject: Question on Pattern
It's not a secret anymore!