Lecture thumbnail 0:00 / 0:00 The kind of implementation of the strategy pattern that we’ve just built is kind of classic, and it’s

very convenient for us to be able to change things at runtime.

So at runtime you can change the output format from one to another, thereby changing the overall operation

of the entire text processor.

And the text processor might be doing other things which do not relate directly to the strategies being

used.

Maybe some of the parts are common to both HTML and markdown, like for example, making sure that the

quotes and the angled brackets are correct, for example.

So we can also envisage a scenario where we don’t really need the dynamic flexibility.

We do want to be able to define different strategies for the text processor to take, but we always

know which strategy we want at compile time as opposed to runtime.

And in this case, having a method like set output format is no longer necessary.

We no longer need it because we can statically define what kind of strategy we want.

Now the question is how do you pass that in?

You can certainly pass it as a constructor parameter or configure your dependency injection container.

So for example, you might have a dependency injection container which says, Oh, can you please register,

for example, the markdown strategy or markdown list strategy and register it as and you specify I list

strategy.

So this is how it’s typically done in the real world.

And this is also in a way dynamic because then you have one central point in the container where it

gets configured where you can actually switch from one to another.

However, if you really don’t want to use a dependency injection container, then you can hardcode the

strategy right into, let’s say, a generic argument.

So here, for example, what I can say is text processor is going to depend on some list strategy called

LHS.

And then here when we come to make a list strategy, we just say new LHS.

Just say new LZ like so.

And of course, for this to work, we need a couple of constraints.

We need to say LZ is an I list strategy.

And it has a default constructor.

But once you satisfy this, everything else is okay, meaning that from now on we can take the example

that we had previously and we can write it differently.

So we can say TP equals new text processor, and here we specify the strategy.

So here we say we want a markdown list strategy like so.

And then of course we can once again do TP dot append list and we can have a new set of items, Foo

Bar and Baz, for example.

And we can write line TP.

Now, this time around, we cannot just do a clear we cannot reassign.

TP Once again.

So if I now say TP equals new text processor

processor of HTML list strategy, it’s not going to work, unfortunately, even though well, here we

go.

Here is the error.

So essentially you cannot assign it because it’s already of type markdown list strategy.

And no, you cannot just make it a text processor of I list strategy because then you’re violating the

constraint that it has a default constructor, for example.

So that’s not possible either.

So we’re going to make a second one.

So we’re going to say var TP two equals new text processor, this time with an HTML list strategy,

and then we can perform the same kind of operation.

So here I’ll just change.

TP to TP two.

And then if we actually go ahead and execute this, we get the exactly the same output.

So this is an implementation of the strategy pattern where the definition of which strategy to use is

static as opposed to dynamic.

So it’s defined at compile time and then you can no longer change it later on.

Play Play Stop Play Start Play information alert