All right so when we looked at the strategy design pattern we saw that what we were essentially doing
is defining an interface and then satisfying that interface and then using that interface inside some
algorithm.
Now the idea behind the template method is also very similar except that the algorithm itself is defined
not in some external class but in a base class.
And then you inherit from it to provide the implementation of the parts of the template method.
So very similar to strategy but slightly different Let me show you how this works.
Let's suppose we have a class called game so we're going to simulate a game between several different
play.
So some of the things related to the game are coalman has a skeleton that is common so let me actually
define a certain amount of public members so I'll define a game which is going to have a certain number
of players.
So it may also make a protected section.
Here I'll have a number of players and I'm going to initialize it in the constructor so that automatically
forces the inheritance to also specify the number of players.
And then I will define the general algorithm the kind of skeleton algorithm for how a game is played.
So we're going to have a function called Run.
And here we're going to simulate the game so we're going to have the storage function which starts the
game and then while the game does not have a winner what we're going to do is we're going to get the
players to take turns and then we will see out that player such and such is the winner which is going
to be done using get winner get winner wins and then line break at the end.
So all of these are read parts of code right now they can be defined as pure virtual members I'm going
to define them in the protected access modifier.
They can be defined as speal virtual members that inherit that can subsequently override and actually
provide their own definition so we'll have virtual void start
start like so and then I will duplicate this and will do the other one so we'll have have winner take
turn as well as get the winner.
But yet when nurturant return an integer indicating who actually won the game winner I also have winner
is a bully and by the way so having implemented this what you can do is you can implement a game of
chess for example because a game of chess is just an inheritor of the general skeleton algorithm and
we can provide the start have winner take and get winner and so on in the inherit.
So here is another thing that I will add in the base classes I will have an integer indicating the current
player.
And that's something that we can reuse in the inheritor as well.
So let's make a game of chess so class chess public game will go.
So first of all I need the constructor.
I must have a constructor so let's actually generate one.
Just passing in the number of places but of course we know the number of players because chess is played
by two players.
So we will put the number two in here.
That's fairly obvious and actually get rid of this particular part.
So we do have to call the base constructor Yes but I'm going to define predefine the number of players
here.
So then let's have the protected section where we're actually going to do those overrides.
So I'll go into avoid functions and I will override the following.
So I need to run.
I need start have winner take turn and get winner.
So I need those functions.
The run is going to be inherited and we're going to be using it inside whatever game that we actually
create.
So for the start of the game I'm going to see out that we are starting a game of chess with a certain
number of players.
So the number of players players like so and then four have winner Well we're going to simulate the
game in a fixed number of turns so we're going to simulate it during a certain number of turns which
I would find here.
So here we'll have the current turn as well as the maximum number of turns.
Let's say 10 for example.
OK.
So what I'm going to do is I'm going to take each turn and when you take a turn we're going to see something
once again turn so-and-so turn turn and then we're going to specify which player took the turn so taken
by player and then current the player.
OK.
So we specify the turn and we incrementally turn variable and we can set the current player to the next
player.
So that's current player current player plus 1 percent number of players.
So that's how we cycle the players.
And I'm just simulating a game here.
So the winner is going to be whatever the current player is at the end of the entire game.
And then the four have winner we're going to see until we reach the final turn.
So return turns because Max turns turn rather.
So we need the turn to reach the maximum number of turns in order to actually have a winner.
So this is our simulation and we can actually run this.
We can use this.
So chess chess and then Chess don't run.
So what's going on here.
Well when you call run what you're essentially calling is a skeleton algorithm which requires you to
have functions start to have when they take a turn and get winner.
However the details of those functions are allowed to be inherited.
So you can see the difference between this and the strategy pattern.
Here you are expected to essentially inherit this abstract class and provide those implementations.
So let's actually run this and see what we get.
All right so as you can see we have the output So we're starting a game of chess with two players we
have the simulation of exactly taking turns and after 10 turns its players zero turn.
So play 0 is the one that wins.
So this is a small illustration of how a template method actually works so the run method.
Here is the template method.
The one whose parts whose constituent parts are actually pure virtual functions that need to be overridden
in the inheritor in order for the method itself to actually make sense.