:00 / 0:00 The template method is another very simple pattern.
And the idea behind template method is that you define a high level description of some algorithm where
you expect the inheritors of this particular class to fill in certain abstract members so that this
algorithm now takes form.
So let me give you a very simple example.
Let’s suppose that you’re in the business of making computer games.
So what you do is you figure out that most of the games have the same algorithm.
They have a start.
You you call some sort of start method, and then while there is no winner in the game, each of the
players takes a turn and then you output the winning player’s name or ID, for example.
So let’s suppose that all the games that you’re working with have the same algorithm.
So what you can do is you can make a template method inside an abstract class to describe this process
and subsequently provide abstract members that the implementers of these games can actually override.
So let me give you an example.
Let’s suppose that we have an abstract class called Game.
Now, most of the game happens in the main game loop, which I’m going to call run.
So in here we want to do a few things.
First of all, we want to start the game.
So notice I’m calling the Start method here.
This is going to be a method that I’ll generate, but instead of having the entire method, I’ll just
have it signature.
So we’ll have something like protected abstract void start.
So this is something that the inheritor would have to fill in to describe what happens when the game
starts and then we can have other members here.
So for example, we can have the ID of the current player so we can have protected int current player,
we can have the number of players protected.
Let’s have it read only int number of players.
This means we have to initialize it in the constructor, which we’ll do so we’ll have an abstract class,
but we’ll have a constructor where you must specify the number of players of this game and then coming
back to the methods we have start, we’ll also have a method for taking a turn so protected.
Abstract, abstract, void, take turn.
So each of the players takes a turn and they can do something like make a move.
For example, if we’re talking about chess or checkers or something like that.
So we also have the let’s have an abstract property so we can have protected abstract, bool have winner.
So we’ll have a Boolean indicating whether the game is actually over and we have a winner already.
And then finally we’ll have the winning player.
So protected abstract int winning player.
This is also going to be a get only property.
So the idea is that we have all of these abstract members and at the kind of high level at the game
class, what we can do is we can define a template method which is going to describe how the game is
going to go.
So the actual name of the template method is run.
And after we start, while we don’t have a winner, while not have a winner, we’re going to take a
turn.
So the player that’s the current player is going to take the turn and then we’re going to write line
some information about the winner.
So we’re going to say player and then put winning player wins.
So this is an abstract class that you’re expected to inherit from if you want to make a game.
So how would this work with a game of chess, for example?
Just going to emulate it here, not really building a chess machine or anything.
So we’ll have a class called Chess, which is going to inherit from game.
And as soon as you do that, you have to implement all the abstract stuff.
So let’s have all of this now.
What would you put in the constructor?
Well, chess typically comes with two players, so we can get rid of this argument and just feed in
the value two in here.
When it comes to starting the game, what we’re going to do is we’re going to write line something.
So we’re going to say starting a game of chess with and then number of players, obviously two in this
case players.
There we go.
Okay.
So when it comes to taking the turn, for example, we’re going to write line and by the way, we can
keep the number, the turn number, as well as put some sort of limit on the number of turns somewhere
down below.
So here I’m going to keep the current turn number private, int turn equals one, and I’m also going
to limit the number of turns to ten private int max turns, max turns equals ten.
So after ten turns we’re going to bail out because otherwise it will be a very long night here.
Okay, so taking turn means that we do some output.
So we’re going to write line turn.
And then ten plus plus taken by player and then take current player.
So turn number five taken by player zero, for example.
And then we’ll change the current player by incrementing it and doing a modulus on the number of players.
Since we are keeping it as an integer, we can do this.
So current player equals current player plus 1% number of players.
There we go.
Okay.
So in addition, we want to determine whether we have the winner.
So essentially we have the winner as soon as we exceed the maximum number of turns.
So here I’m just going to do an expression.
So have winner is going to return whether turn is equal to max turns like so.
And finally we have winning player which when we’re done is going to be the current player.
So what have we done here?
We essentially defined a template method in the base class, and it’s composed out of several abstract
invocations.
So we have abstract calls to start have winner take turn and winning player.
And these are declared as abstract here, which means that in the derived class you have to implement
them, you have to give them some concrete meaning.
And we have done so in the chess class.
So now what we can do is we can actually build a game.
So I can say var chess equals new chess like so and I can do chess dot run.
And what this is going to do is it’s going to invoke this run method and it’s going to call all of these
things and these things are abstract.
So they’re going to be taken from the chess class and we will invoke all of the right pieces of code.
So let’s actually execute this just to see what it is that we are getting.
All right.
So as you can see, we’re starting a game of chess with two players and then the player turns are alternating.
So it’s player zero, player one, player zero player one.
And the final player is player one and that’s the player that actually wins.
So this is the gist of the template method, a very easy idea of describing the algorithm in this case,
the game playing algorithm at a high level, but letting the inheritors provide the implementation details
for the different constituent parts.
Play Stop Play Start Play information alert