Hello.
In this lecture, we're going to talk about Question 40, what is the template method design pattern?
Template method is a design pattern that defines the skeleton of an algorithm in the base class.
Specific steps of this algorithm are implemented in draft classes.
Let's consider the following example We are developing a platform that allows users to play board games
online.
The first board game we deliver is suckers of cartoon.
He was the slightly simplified implementation.
He was the main method in this class to play this game.
We must first set a bird.
Then we play turns until the game is finished.
Finally, we must select the winner.
So let's see a game.
All right.
In this case, the game was finished in six turns.
Soon after, we are asked to implement another game, this time it's terraforming Mars.
Huh.
It seems this game is quite similar to the code we've had before after implementing a couple more board
games.
What come to our revelation?
All board games follow a similar template.
We first set up the board, then we play turns until the game is finished and finally we select the
winner instead of repeating this logic in each class.
We could define it once in the base class and ask the subclasses to only provide the details of the
implementation of each step.
This way, if the template changes for some reason, we only have one place to fix.
We'll be using the template method cheese and butter.
First, let's define the template itself.
It will be done using an abstract class.
Here we defined the template, but we made all those methods abstract.
It will be up to concrete classes to implement them.
Let's make the games we defined derive from this abstract class.
Now, the feeling that those cases had in common, so the general template of it game is engrossed in
the base type, if this template changes will only need to adjust this base across.
The derived classes only define what makes it boardgame special, and they don't replicate what they
have in common.
The template method design pattern is useful everywhere where some base algorithm is needed, but the
specific parts of it, very a practical example, could be the execution flow of tests in some units
test framework.
Typically, such execution looks like this for each test.
We run the setup method, then we execute the test and then we run the teardown method.
This could easily be achieved with the template method, the design pattern.
First, let's define the base class for all test fixtures.
All right.
So here is the template of the test picture for each test that should be wrong, it first runs the setup
method, then it executes the test and raises the counter of failed tests.
If the test was not successful.
And finally, it runs the cool down method.
The final result of the execution is true only if the count of three tests.
This is zero.
It's up to the draft classes to define what this exactly should be wrong and what happens in the setup
and teardown methods.
So let's define some actual tests now.
First, let's see the class will test it here.
As you can see, it's not very complicated.
So the test fixture.
Testing this class will be derived from the base, the structure across.
All right.
Here we defined two simple tests for the calculators admitted in the setup method.
We simply create the class under test object.
And in the teardown, we just bring something to the council.
Let's see if those tests will pass.
Great for both us.
First, the setup method was executed, then the test itself and then the teardown method thanks to
the template method.
We found this common behavior in the base class and in the derived class.
We could focus on what's important.
So on the tests themselves.
As you can see, the template method design pattern can be quite handy everywhere, where a generic
algorithm shall be defined once.
But the implementations of the particular steps of this algorithm may vary when discussing design patterns
during the interview.
You can be asked What is the difference between the template method design pattern and the strategy
design pattern?
Both patterns allow specifying what concrete algorithm or a piece of the algorithm will be used.
The main difference is that with the template method is selected at compile time as this pattern uses
inheritance with the strategy pattern.
The decision is made at runtime as this pattern uses composition.
All right.
We learned about yet another design pattern.
Thanks for watching and see you in the next lecture.