All right so what are we going to do here is we're going to implement a strategy for printing a list
of items in different formats and the formats we're going to support our markdown and HMO.
So let's have those in class formats marked down and HVM.
So here is the idea let's suppose that we wanted to print a list.
Now what do you do when you print it list in markdown.
You just put a star and then your list every time you put another star and then your list item but in
HDMI you actually have to put the opening and closing lists tags before you add each of the items.
So here is a list item for example so it's different in marked down HMO but there are common things
like you have some sort of way of adding individual items that would be this part but in HMO you also
have the preamble and the closing kind of tag as well so we're going to have three different methods
inside our strategy for printing a list.
So here is a list strategy.
So all we have to be able to print different things we have to be able to print the start of the list.
So I'll say virtual void start.
Let's suppose we're going to be printing to an postering stream.
I will actually not make it pure virtue.
Instead we'll have it with a kind of default implementation because sometimes you don't need that implementation
like for example the markdown does not need to implement start.
And so Notice I'm using the interface segregation principle here in a very clever way.
So I'm I'm letting the inheritance actually avoid implementing things which they don't need.
So I will have the same thing for the End Function.
So I'll make it virtual But I will not make it pure virtual.
And then of course we have the list item for actually adding the individual items.
So once again virtual void and a list item where you have postering stream says and also Conte's string
item.
Now I could make it pure virtual and this one because essentially every single implementor has to do
something about this one they do have to add the item.
There's no avoiding it basically.
So now what I wanted to do before we make the different strategies is I want to make a text processing
component which is actually going to help us print those lists in a proper fashion.
So that's how a text processor.
So a text processor is going to be something which will allow us to use a particular strategy in order
to print a list of items.
Now the question is how do we actually refer to the strategy itself.
There are two approaches to this pattern the dynamic approach where you simply have a variable of a
strategy type and the static approach where you take it as a template argument.
We're going to go with a dynamic approach in this lecture and we're going to look at the static approach
and the next one so here when I'm going to do is I'll have a private section.
Well I have the whole string stream that we're going to fill in and I'll have a unique P-T are to a
list strategy
list strategy and this way will also have some functionality of actually specifying what kind of striver
you want.
Now what the way we're going to do this of course is by using that in that we have.
So we have an income for output formats and we'll have a function here called set output formats where
you take the output formats and then we can switch on switch formats and depending on the format we
make an instance of the appropriate strategies so let's create the missing case labels here.
OK so in the case of markdown what we're going to do is we're going to see a strategy list strategy
is equal to make unique markdown list strategy.
And we're going to do a similar thing for the for the other strategy now.
I haven't created marked down this strategy.
Let's actually stub those out here struct markdown list strategy list strategy.
And let's actually duplicate this because we have the DML list strategy the list travel.
So these are the two strategies that we're going to be using And here once again I will say Liszt's
strategy he calls make unique Scammell list strategy.
So with a break in here.
So this is how we're going to switch from one to another.
So this is a dynamic approach you can actually switch from one printer to another at runtime.
Absolutely no problem here.
Let's see.
Why is it complaining about this saying unreachable code.
Well how bizarre.
Oh yeah.
Put the break in here.
That's silly of me.
OK so we are now ready to go but we need some functionality for actually appending a list.
So I'll say append list and I'll just take a vector of strings items and then I'm going to add them
all.
Using the strategy so this is where you actually use the strategy we have this interface of strategy.
And we're going to use exactly that to print the list whatever that is so I'll say list strategy.
Start with the postering stream and then for each of the elements I'm actually going to print that TELLEMENT
Let's see what's going on here.
I was saying I'm playing opera.
Oh yeah that's a sea lion bug and then for each of the items in items what we're going to do is we're
going to take the strategy again and we go into a call list item with O assassin and providing the item
itself and then we'll do the end here.
And I was just like so.
So this is how you actually use this strategy also make utility function here for clearing the text
Proceso so I get to demo the different printers.
So I'll say Oh assess as to your empty system clear that again.
So this is how one would clear the whole thing and we can actually now experiment by printing different
kinds of lists we can print the same list in both marked down form as well as the demo form.
So here is a list of items.
Let's have food bar baz.
And then here is the text processor.
First of all I'll set the output format to mark down
and I will append a list of items and then I'm going to see out what's what I get as the end result.
Now let's actually expose the holstering stream So here I'll have a string to your return.
Oh SSTO this.
Here we go.
So here I will do all this.
These are followed by a line break and then because it's dynamic strategy you what I can do is I can
switch from one strategy to another so I can see that clear.
Well actually I need the clear because I have that list in here.
But yeah let's let's clear it again.
So let's set a different output format let's say output formats.
Now this time around and what I'm going to do is I will once again append the list in the list of items
and I will once again printed out just as I have here.
OK so the one thing that we need to do before we actually run the whole thing is to actually implement
the appropriate API.
So here I'm going into override functions and for the markdown strategy what I need is I only need atlast
items so that's the only thing that I care about.
And here I would just say Oh.
SS Then I'll put a star like this and then I would put the actual item and then maybe a line break.
That's it.
That's how you add a single list item but for the HD a mousetrap that you actually need to override
everything.
I need both the start and the end and everything in between.
And here is where we're going to add the opening and closing tags as well as handling the individual
items.
So here I will say oh yes let's put in the opening tag followed by a line break and I will do the same
here with the closing tag and for the list item what we're going to do is I will say oh assess and let's
actually put a couple of spaces in here so we'll have the list item.
I will have the item itself.
We'll have the closing tag and the line break.
OK so I think we're in now finally good to go we can actually run this example and see what the output
is.
All right.
And as you can see we're getting the correct output from both strategy so we're printing the list in
marked down fashion than we have the stars and the items.
And here is the strategy of the M-L princeling being applied to that same list.
So this is how you can implement a dynamic approach to the pattern because here we can actually switch
from one pattern to another runtime.
And in the next lesson we're going to take a look at the static alternative.