Lecture thumbnail 0:00 / 0:00 Just in case you are fans of functional programming, I wanted to show you the functional programming

equivalent of a template method.

So instead of using inheritance, all that happens here is you simply stick every single invocation

as an argument to some sort of static method.

So here in the run method, instead of using inheritance and using those virtual overrides, what’s

happening is you are expecting to be provided a number of arguments.

So you’re expected to get an action for the start of the game.

You expected an action for taking a turn in the game.

And then there are functions for determining whether or not you have the winner as well as a function

for returning the winning player.

Assuming that the game is done.

Now, the implementation of the template method itself is very similar.

In fact, it’s almost identical to the implementation we had when we looked at the inheritance approach.

But it’s the usage of these functions and the use of the template method is actually different.

So here we are in the main method and inside the main method you’ll notice that we’re not declaring

any additional classes, we’re not working with classes at all because this is purely a functional approach.

So the different members of the game class would now be represented as simply variables, and then you

would have a bunch of closures.

And these closures can of course refer to these variables.

They can capture the variables and work with them.

So we have similar functions such as start have winner take turn and winning player, but these are

nested functions inside the main method.

So it’s kind of like a methods within methods, so to speak.

And there is no problem doing this in C sharp.

So the end result is that you run the template, but you’ll notice that instead of using inheritance,

all you’re doing is you’re taking these nested methods or nested functions and you’re passing them into

the template method.

So you’re passing references to all these four methods, which in turn when we take ourselves back here,

these are going to be invoked one after another.

And of course any kind of variables that they capture will be treated correctly because remember, these

methods, they have references to things like variables like this would refer to current player, which

is defined here.

But because Dotnet makes it easy for us to capture variables in lambdas, there’s really no problem

in well, not lambdas here, but just just nested methods.

There’s no problem in doing it like so.

So this is an alternative to the template method.

It’s obviously somewhat less structured, somewhat less organized.

But if you do want to confine the operation of a template method to just a single method like the main

method here, for example, then there is no reason for you to spawn a separate class.

There is no reason to use inheritance at all, although of course you do have to use classes like here.

We’re using a static class called game template.

You could take this a static method and you could also move it into my demo class.

That would work just fine.

So in theory, you could get rid of this class, but there is no way of getting rid of classes altogether.

But this is the functional approach.

So this is the approach where you would have some functions being provided here which do not take any

arguments and don’t return anything.

And you have some functions which do in fact return things like a boolean here and an integer there.

So the signatures of these methods as they are provided into the template method are fairly obvious.

You know what you need to provide for every single one of these parameters.

And so then you simply feed in these parameters and you get you get the operations done in pretty much

the same way as we did when we used inheritance for the template method.

Play Play Stop Start Play information alert