Lecture thumbnail 0:00 / 1:50 In this section of the course.
We’re going to take a look at the strategy design pattern.
And this basically allows you to partially specify the behavior of the system and then augment it later
on.
So that may have sounded a bit cryptic.
So let’s talk about what this is all about.
So many algorithms can actually be decomposed into what I would call the higher and lower level parts.
So, for example, let’s consider the process of making tea.
So the process of making tea can be decomposed into something high level, like the process of making
a hot beverage.
I mean, it doesn’t matter whether you’re making tea or coffee or any other hot drink, you have to
boil the water and you have to pour the water into the cup.
So that is the high level part.
And then you have the tea specific things.
So after you have done the high level stuff, what you can do is you can take a tea bag, put it into
the water, you can add milk if you’re British or you can add lemon or something like that.
So the high level algorithm, which we specified up above the process of boiling water and pouring it
into a cup can then be reused.
So the tea specific things are tea specific, but everything else can be reused for making something
else like coffee or hot chocolate, for example.
And this is supported by the beverage specific strategies.
So that is where the strategy pattern actually comes in.
So the strategy design pattern essentially enables the exact behavior of a system to be selected either
at runtime.
So there are two implementations, as we’ve seen with many design patterns.
This either a possibility of specifying it at runtime, the dynamic strategy or at compile time, and
that would be the static strategy.
Now this pattern is also known as policy in many programming languages, including especially the C
plus plus language.
Stop Play Play Play Start Play information alert