We’re now going to take a look at yet another design pattern.

And this one is called template method.

And it’s all about providing, once again high level blueprints of an algorithm to be completed by its

inheritance.

Now, this might seem familiar to you because we’ve seen something like this already in the strategy

pattern.

So essentially we know that algorithms generally can be decomposed into the common parts or the high

level parts, as well as the specifics.

And the strategy pattern already kind of handles it for us.

So why do we need yet another design pattern?

Well, the strategy pattern does this through composition.

So essentially you have the high level algorithm which uses some interface, and then the concrete implementations

actually implement this interface and then you stick them in and it works.

The template method is very similar, except it does this through inheritance instead.

So the overall algorithm would typically be an abstract base class.

And then of course what you have is you have inheritance of this algorithm which overrides certain key

abstract members.

But the base class actually keeps the template for the actual algorithm.

So the parent template method, the method, which is non abstract, is then invoked to actually orchestrate

the algorithm at the high level.

So the template method design pattern quite simply allows us to define the skeleton of the algorithm

in the base class with concrete implementations defined in the subclasses.

Stop Play Play Start Play information alert