So if you think about an application such as PowerPoint or any kind of vector drawing application you
know that you can draw and drag individual shapes around but you can also group shapes together.
And when you group several shapes together you can treat them as if they were a single shape so you
can grab the entire thing and also drag it and resize it and whatnot.
And we're going to implement the composite design pattern around this idea of several different shapes.
So let's suppose that we define some sort of base class we define a structure called a graphic object
which is drawable So the only thing you can do with it is draw.
So defining pure virtual function called draw here and now we can have concrete shapes like for example
you can have a circle so I can make a circle which is a graphic object.
And what I can do is I can go ahead and implement the draw function.
And here I can let's just emulate that we're going to be drawing things so I will see how the fact that
this is a circle.
There we go.
Now in addition what I want to be able to do is I wanted to group several circles together and put them
into a single selection that I can also draw as a whole thing so to speak.
So for this what I'm going to do is I would define a structure called Group which is also going to be
a graphic object.
And here what I'm going to do is well I'll give the group a name.
So we'll have the name of the group and now actually initialize it in the constructor.
So let us go and initialize the constructor like so and also I'll implement of course the draw function.
And here what I would do of course is I would draw each of the individual shape so if I'm working with
a group I need to draw the group which means drawing every single one of those shape.
So first of all let's output some information about the group so I'm going to say this is group called
a name and it contains the following.
So it's going to be a bunch of elements that we're going to output so contains and then an end line
and then we print out all the individual elements.
I'm going to have a for loop where we go through each of the objects and we simply take the object and
draw it.
Of course I haven't defined the objects yet so let's stick them up here.
So here I'm going to have a vector off graphic object pointers.
We're going to use pointers for this one Saltcoats objects and then we can iterate them and we can actually
take each one of them and draw each one of them individually.
OK so what does this all give us it gives us the ability to define both individual shapes as well as
to put them into a group if we want to.
So that's how a group I'm going to call it routine is going to have the name roots as well.
And then I'll make two objects so I'll make two circles see one and see two.
And then I can add.
See I can and the first object to the group so I can say rootes dark objects Daut push back and I can
add a pointed to see one for example but we can also have groups of groups.
That's the great thing about the composite design pattern because groove also inherits from graphic
object.
You can take a group here and stick it into objects and this can go on forever.
So what I can do is I can make another group so let's go here and I'll make a subgroup I'll call it
solve.
And then I will add the second circle to this group so I'm going to say subgroup Daut objects.
Push back and I will add a pointer to C2 on this one.
Now of course I can add the subgroup to the group so I can say roots Dauth objects doth push back and
get a pointer to the subgroup like so.
And this is a set up that we can actually draw so we can take the root object the top level object and
I can say root draw we can actually see what we get as we kind of compile and run this.
All right so as you can see what's happening here is we have a group root which contains a circle as
well as a group called sub which also contains a circle.
So it's this kind of recursion that go on forever.
And essentially this basic idea of composition.
So you have an object which is scalar but you also have an object which is a composite of other objects
but they have the same API.
So the overriding idea of the competent design pattern is that individual objects as well as collections
of objects all behave in the same manner and here it's enforced by the interface so we have this graphic
object interface with its draw method and this Draw method is what allows us to use both circles as
well as groups in a uniform manner.