8

Similarities and differences between the two:

Template Method

  • Relies on inheritance.
  • Defines the steps of an algorithm, and leaves the task of implementing them to subclasses.

Factory Method

  • Relies on inheritance.
  • A superclass defines an interface to create an object. Subclasses decide which concrete class to instantiate.

The two side by side:

enter image description here

I'm not sure what the phrase "Factory Method is a specialization of Template Method" means (it's in the Head First Design Patterns book). In Beverage we have the method prepare which is final and defines a series of steps. In PizzaStore we have a method which is abstract, and subclasses redefine it. How is the latter a specialization of the former?

  • 4
    I see a lot of downvotes on this post from the pattern police. There's really no reason for that; you now have three people who say that the phrase is probably ill-advised. Note to readers of this post: don't believe everything you read in a book or online. – Robert Harvey Jan 13 '17 at 14:58
5

In Beverage we have the ("template") method prepare which calls some abstract methods like boilWater, brew etc. which are implemented in the subclasses. It is not essentially for the pattern to have the template method located in the base class, it could be anywhere else, assumed the abstract methods are public. But it is essentially that there are the abstract methods which must be overriden and will fill the gaps in the "algorithm template".

The factory method is also about a class where abstract methods are overwritten, a core point is the caller of that method does not need to know the exact type of the created object. Note that in PizzaStore, the abstract method createPizza must be called somewhere from - lets say a method createLotsOfPizza.

This makes the latter a template method, where the specific steps of the "algorithm to create pizzas" are the gaps to be filled. Now it is probably just an imprecise use of words by saying "the factory method pattern" is a special case of the "template method" pattern. Especially the "factory methods" are not "template methods", however they might be called from a template method. So to be more precise, we might say

"the factory method pattern is typically used in conjunction with a special case of the template method pattern"

and I guess that is what the authors of that book wanted to express.

0

I think "Factory Method is a specialization of Template Method" is a mis-characterization. I believe that it would be more accurate to say that the Template Method pattern is merely a variation on the Factory Method pattern.

Further, I claim that the Template Method pattern isn't really a pattern at all (in the Gang of Four sense), since it arises naturally through inheritance and method overriding. You can see that, in the diagram above for the Template Pattern, the descendant classes merely override methods in the base class with their own behavior.

On the whole, Software Patterns (of the Gang of Four variety) are really just workarounds for deficiencies in programming languages, and the Template Pattern only restates what object-oriented languages are already natively capable of.

  • If you're using a definition of a design pattern that makes Template Method not be a design pattern, I think your definition is wrong. Design patterns are abstract ideas that convey relationships between different parts of your code, and have nothing to do with what language they're implemented in. Design patterns aren't implementation details, they're descriptions that can be used at a high level in order to communicate the design decisions that have been made concerning part of a system. – Jules Jan 13 '17 at 3:12
  • More specifically, Template Method is a particular way of using inheritance that is useful in some circumstances, but it is clearly a design decision as there are other ways of achieving the same purpose (the same effect could be achieved, for instance, using aggregation, in which case it would probably be considered an instance of the Strategy pattern). Given that a decision is being made, a name is important in order to allow us to talk about that decision, and where we have a name like that it is a design pattern. – Jules Jan 13 '17 at 3:15
0

I would say Head First is simply wrong in this characterization.

Wikipedia defines Template Method as:

In software engineering, the template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses.

(It references GoF for that.)

And Factory Method is defined as:

In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.

There is no overlap between the two, except that in C++, inheritance is a key part of both. However, in languages that make a distinction between implementation inheritance (extends in Java) and interface implementation (implements in Java), even this commonality doesn't exist, since interface implementation is sufficient for factory method, but not template method. And in C++, pure implementation inheritance (without the interface inheritance, i.e. private inheritance or CRTP) can be used for the former, but not for the latter.

And aside from that? One is a behavioral design pattern, the other a creational pattern. One describes an outline of an operation, with some key parts left out to be filled in by specialized subclasses; the other specifies a very simple contract, leaving the entire implementation to the concrete instances. They are completely distinct.

In my opinion, the line as quoted is simply wrong. Either it is a glaring error in the book, or (less likely) it is out of context and should be interpreted differently.

  • I think the statement is not plain "wrong", it is just a sloppy use of words. See my edit. – Doc Brown Jan 13 '17 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.