The dynamic decorator is great if you don't know which kind of decorator you want and you want to be
able to pick them at runtime but sometimes you know the decorator you want compile time in which case
you can use templates and as a result you can also get additional benefits like remember when we had
the colored Toka we couldn't call resize on it because the interface didn't incorporate it.
So now we're going to fix this problem and in order to do this we're going to use the approach of mixin
inheritance in order to build different kinds of decorators.
What I would call static a decorator so we're going to have color shape to a transparent shape too and
they're going to be implemented in a completely different way.
So let's begin with colored shape too.
I'm going to go slowly here because I want you to understand what's going on.
So we'll have a Template structure template type name the structure called colored shape too.
Now the idea is that instead of aggregating the shape which would be the template argument you actually
inherit from it because in C++ you can inherit from the template argument and this is called mixin inheritance.
So here I'm not going to have a member code shape or a reference to that member because we already are
that member.
We are using inheritance.
So one question you might have is well how do you make sure that T is in fact a shape as opposed to
something else.
Well there is no real way of kind of asserting that apart from maybe a static set.
So if you wanted to you could add a static assert here the way you would say that you would use is base
off to determine the shape is the base of T.
And you would sort of assert on the value and if that is false then you could say templates argument
must be a shape.
That's the only thing that you can do in terms of making sure that T is a shape.
Now the compiler isn't going to know that he's a shape until you actually start substituting ties.
But from now on what you can do is you can start using the same kind of functionality that an ordinary
color shape would give you.
So you would have the color here you would have some sort of constructor and I'll put in an empty constructor
and you would have an implementation of the SDR function.
Once again you cannot just go all to insert and implement functions.
Now I'm going to give you a new thing because the idea does not know that he is off type shape.
So given this what we can now try to do and by the way I made a small mistake here should be colored
shape what we can do is we can implement SDR by hand.
So here it is now see line is going to complain that I'm overwriting something which doesn't exist.
You can see the red wavy underline here but trust me it's the correct way to go.
I do know that T is always going to be a shape so overwriting SDR is always going to be the correct
solution.
So let's have postering stream of SS here and then return Sesto last year and now we need to do is we
need to once again call the base class implementation of SDR and then add something to the end.
So here I'm going to take o s s.
First of all to call the base class you take the base class which is t and you just say t colon colon
s.
TR That's how you call the base class.
And then you say has the caller and you provide the caller no go so this is the implementation of SDR.
Now the only challenging part here is the implementation of the constructor because remember if you
making let's say you making a transparent shape of a colored shape of a square so you're going to be
writing something like the following.
Well with the number two in both these cases.
So are you going to be writing stuff like this.
And you really want the constructor arguments to allow you to set all the different parts so you want
to set the transparency you want to be able to set the color say red and you want to be able to set
the size of the square itself.
So you want all of those constructor arguments to be here.
Which means that at every point of inheritance you have to forward them.
Well luckily with modern C++ you can actually do that thanks to variadic templates and forwarding.
We can actually do this so this is what the constructor will look like.
So first of all we'll have a template argument where are we going to have a pack here so we're going
to have the arguments all packed together.
And here is the constructor for college shape too.
OK so here we're going to do two things.
First of all we take the argument which is relevant to this decorator which is the class all take string
color.
And then of course we take the other arguments.
So here are the other arguments and then we initialize them first of all we have to call the base class
with the rest of those arguments so the way this is done is once again the base class is of type T.
So we're going to be calling t.
And here we want to forward the arguments so we'll use as the D forward with the template type of args
and we're going to feed in the args and we're going to put in ellipses here so that they are all nicely
rolled out as they are spread across the entire inheritance hierarchy.
And the second argument is color so we just initialize it with the color that's been provided.
And that's our constructor.
So not the simplest constructor in the world but this is the constructor which will allow us to kind
of group data together.
Now in a similar fashion what we can do is we can implement transparent shape too.
I'm not going to bore you with actually typing all of this out.
Instead let me just paste it in here.
So a very similar implementation we have the transparency we have the SDR implementation.
We have this should be all right.
By the way.
OK so this is how things are set up.
Now try combining all of these decorators together.
So this approach of mixing inheritance Well it lets us do is the following so I can make let's say a
green circle so I can make a colored shape to of circle called green circle.
And here I can specify the arguments so the first argument is the color which is green and the second
argument is the size of the circle which is five.
Now I can see how this circle for sure green circle but where I can also do is I can call an API member
on this circle.
And by the way yes this needs and ask here in order to print correctly what I can do is I can take a
green circle and I could resize it and remember with a dynamic decorator that's one of the things we
couldn't do because the decorator lost the type.
It only had access to whatever the interface satisfied.
Here we can call the base class because essentially we're using inheritance instead of aggregation.
So here I can resize the circle by a factor of two.
So that's actually execute this and see what we get as the end result.
Arrived so we have a circle of radius 10 which has the color green and this is consistent with what
we actually wrote so we made a circle with radius 5.
But then we used the resize method to double the radius and so we got this green circle and it printed
correctly and we can continue this idea of composition and actually make let's say a transparent shape
so I can make a transparent shape to all of colored shape to have square for example.
And this is I'm just going to call it square and here we can provide certain arguments I can provide
some transparency value I can provide the color blue in this case I can provide the size of the side
of the square and once again we can print it out.
So I ask you square as SDR and line.
There we go.
OK.
So let's see what this gives us.
Fantastic.
So as you can see we've made a square with side 10 which has the color blue and 20 percent transparency.
So essentially what this example demonstrates is that if you're prepared to give up on the dynamic composition
nature of the decorator and if you're prepared to define all the decorators at compile time you get
the added benefit of using mix inheritance.
And that way you actually get the members of whatever object you are decorating being accessible through
the decorator.