We are now going to take a look at the implementation of the decorator design pattern and there's actually
two different implementations that you can have you can have a dynamic decorator or a static decorator
and this is actually this set up the division between dynamic and static is something you're going to
see in other design patterns as well.
So we're going to start by looking at the dynamic decorator.
So let me put forward a scenario that we are going to consider.
So I'm going to have a base class called shape so I'm going to have a truck called Shape and this structure
is going to have just a single virtual member so we're going to have a virtual string SVR Konst equals
zero.
So this is the member that we're going to have.
OK so now I can implement this in a concrete class such as a circle so I can have a circle which has
a shape.
And here I can specify for example the radius of the circle.
I can have some constructors Solas generate a couple of constructors.
Let's have one like this.
Let's also have an empty constructor I need to use the mouse for this one unfortunately.
And then of course we need to implement the actual interface implement the SDR.
So here I'll use the output string stream to actually just put together some information about the circle
in this case all I'm going to say is I'm going to say that this is a circle of radius and specify the
radius and I will return 0.
SS So this TR That's the string representation of a circle.
So that's what I'm going to do for now.
I will also give the circle some of its own members which are not part of the interface so for example
you might have an interface made book called resize which takes a scale factor and simply multiplies
the radius by that factor.
So radius gets multiplied by this factor.
There we go.
OK so having made a circle we can also make a square in a similar fashion so a struct Square which is
a shape.
Now once again for the square you might have the side of the square and once again you can have all
sorts of constructors like your constructor initialising the side you can also have a constructor which
doesn't really do anything just an empty constructor.
And then we need to implement the SDR member.
And in here I'm just going to copy this implementation because it's actually very similar.
We're using it as a form of diagnostics so here it is going to be a square with side and then specifying
the side here.
OK so we have a hierarchy of two different shapes and what we want to do is we want to enhance this
hierarchy.
So let's suppose that we have the square and the circle.
And we want to add color to it.
Now we're suddenly not going to create two other classes we're not going to create a closed circle and
a colored square.
That would be too much.
We're just going to have a color shape.
So the way we can implement this is as follows We can make a new class let's call it color shape.
And this is going to be a shape as well so it conforms to the shape interface and here we're going to
take a reference to the shape that we're decorating like so.
And we'll also specify the color and then we can make a constructor's So here will stick.
Let me actually hide these windows.
I will stick in a constructor which initializes both the reference as well as the color and then we
can outputted by implementing SDR.
So once again just go until the implementation and here I will once again use 0 string stream SSN and
return Sesto SDR.
When I'm done with it and what I'm going to output is slightly different because I'm going to be using
the SDR implementation of the aggregate chave the one that I'm using here plus I'm going to add some
information on the site so I'm going to say oh yes yes and I'm going to say shape though the SDR.
That gives us the baseline kind of implementation of SDR.
Most are going to say that it has to color and specify the color right here.
So this is a fully fledged dynamic decorator and the reason why it's dynamic is because you can create
this decorator out runtime you simply instantiate a colored shape provide the arguments and you're done.
And that's exactly what we're going to do right now.
So here with I'm going to do is I'm going to make a square called Square and it's going to have a side
of 5 for example and then I'm going to make a colored square so I'm going to have a color shape color
shape less code red square and this is going to take this square that's being decorated and I'm going
to specify the color as rather than we can actually print both of these objects.
So I'm going to print both the square as well as the Red Square.
There we go of course we can print anything yet because we need to generate some sort of.
Well actually let's not generate any kind of stream map with operators let's just reuse SDR.
I was going to generate this tree map with operator as well but we have the string output here so we
can try this out.
We can actually try running this square as well as decorator see what we get.
OK so as you can see on the first line we get a square with side five and then the second line we have
a square with side 5 which also has the color red.
Now so far we have just a single decorator so we may as well add yet another decorator just to see if
you can in fact mix decorators and I can tell you in advance that you can at least in the scenario that
we're considering here so we're going to have a transparent shape so a transparent shape is also a shape.
Once again you have a reference to the underlying shape and you also have a let's have a end date on
the score.
Transparency.
So once again I'll generate a constructor which initializes both of these and then generate them more
interesting as to your implementation.
So here I'm going to have an SDR implementation where I actually convert the transparency value into
a percentage.
So that's going to be a lot of fun so we'll have those dreams dream SS once again we turn the SDR at
the end.
But here what I'm going to do is first of all I'll use the baseline implementation of Shabdo SDR once
again.
But after this or this is where the fun begins so we have house and then we can i don't like the way
that we actually condense things.
Really horrible.
So here we can for example take the transparency value turn it into a flow and convert it to a percentage
so let's do this quickly so all static cast to a float to cast transparency.
Can I please have code completion here.
No.
Clearly I mean giving me everything except what I want so divided by 2 5 5 F and multiply by 100 F that
way I get the percentage value.
And so this is going to be it's going to be percent transparency.
There we go.
OK so I now have a second decorator and let's see if we can actually combine these decorators together
because essentially as you can see this decorator just like the color shape decorator who takes his
shape and that shape can be a decorator because guess what both of the decorator s they actually inherit
from shape.
So what I can do is I can make a square which is red and partially transparent.
So let's do that.
So I'll have transparent shape I'm going to call it my square which is going to take the Red Square
as the base line and will also specify transparency like for example.
I think this is one fifth of the transparency so we're going to get 20 percent transparency here so
here I can see out my square or the SDR and line and this actually take a look at what this gives us.
So as I'm sure you can probably guess this is going to be both colored as well as transparent.
And we're going to get a longer presentation of the object and that's exactly what we get.
We get a square with side five which has the color red and has 20 percent transparency.
So this is great this is how you actually build a decorator.
As you may have guessed you can make a transparent shape from a color shape like I'm doing here or you
can make a color shape from a transparent shape.
And yes by the way you can also do crazy things so you can for example make a college shape out of college
shape out of college shape out of square.
This is also allowed now.
There is one downside to this approach.
So let's suppose that instead of a square I'll just change it to a circle.
I'll keep the name as it is or maybe I can maybe I can actually rename it.
Renaming is so cool.
Like so.
OK so there is a bit of a problem if you look at the definition of Serko we actually go to the definition
you can see that the circle has a member called resize.
And certainly if you have a reference to a circle you can say circle dot resize and everything is OK.
Unfortunately as soon as you decorate it you're restricted by the interface that the circle implements
which means that if I take well in this case let me actually rename this to red circle.
So if I take a red circle for example and I try to call resize on it you can see it's not part of code
completion because it's not part of the shape interface so this is a limitation of the dynamic decorator
it does allow you to build all sorts of declarations at runtime because remember each of these can be
initialized at runtime.
But the downside is you don't have access to the underlying API of whatever object you're actually decorating
unless it's part of some interface or other.