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?

  • 6
    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. Commented Jan 13, 2017 at 14:58

4 Answers 4


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 essential 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 essential that there are abstract methods which must be overriden to 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.


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.

  • 1
    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
    Commented Jan 13, 2017 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
    Commented Jan 13, 2017 at 3:15

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
    Commented Jan 13, 2017 at 13:56

"In my opinion, the line as quoted is simply wrong. " >> I cannot agree the GoF probably talked about similarities:

  • In template method the methods called inside the body are implemented in subclasses. In factory method implementation is provided in subclass. So if brew() had returned an object it would be also a factory method. Most likely PizzaStore::createPizza will be called from the some template method when handling the order.

An argument about extends/implements is totally irrelevant as someone mentioned design patterns are concepts not linked to any language syntax. esp. in JAVA where there is a 'default' method in interface so you can achieve it also using interface.

So it depends on the view if someone is biased to see just some minor differences. But if you are keen to find the common features then yes both patterns defer implementation to the subclass. In first case it is a step's implementation, in second case it is the implementation of and object. Both implementations are then used in super classes.

  • Instead of posting it as an answer, you can simply comment it on Sebastian Redl's answer. In this way he will be notified and you guys can clarify the confusions. Commented Apr 12, 2022 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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