Lecture thumbnail 0:00 / 0:00 In this example of the composite design pattern, we’re going to emulate a drawing application.
And if you remember drawing applications, you know that you can draw different shapes like squares
and circles and whatever, but you can also group shapes together.
And once you group several shapes together, you can drag that entire group.
So that’s what we’re going to implement using the composite design pattern.
And the way I’m going to do it is a bit peculiar.
So I’m going to have a base class called Graphic object, Graphic object, and then I’m going to inherit
from this class and I’m going to have a class called Circle, which is going to be a graphic object.
Okay, let’s try this again, graphic object, and then I’m going to have a square, which is also going
to be a graphic object.
There we go.
Okay.
So now the graphic object will have certain properties and now have both properties as well as public
fields as well.
So we may have a public string colour, for example, for the colour of that particular shape, but
also we might want to have its name so public and I’ll make it virtual string name with get and set
and I will default this to group.
Now what’s going on here?
Well, what’s going on is that I’m using the root class to act as a group for any kind of member, which
is part of that group so that you can drag them together and whatever.
So now these graphic object, since it’s a group of things, can have an optional set of children.
So for this we’ll make a lazy field.
So I’ll have private lazy list of graphic object, graphic object and I’m going to call this children
equals new.
And let’s just use the default initialization here.
And then I’m going to have a property which exposes those children.
So public list of graphic object children, which returns of course, children dot value.
So this way we’re using the lazy type correctly.
We’re only instantiating the list of objects if in fact we do need to have children in this particular
object, in this particular group, I might add.
So the idea then is that we have some way of actually printing all of this to the command line.
And we also have the name which is virtual, so you can override it.
So here, for example, I can go into override members and I can select name.
And instead of this bulky override that we’ve generated, I’m simply going to make sure that I return
a particular value.
So here I’m going to return circle and in the other.
So I might as well write it by hand override string name, which is going to return a square.
And then of course the color which we have will be configurable from the client.
All right, so let’s implement some sort of two string for the entire graphic object.
We can just implement this in the root here.
So I’m going to override to string, but we need to find out the depth because we can have groups containing
groups and so on to infinity.
So I’m going to also delimit depth.
So I’m going to have an overload of sorts.
So I’ll have another method here, private void print, which is going to take a string builder where
we’re going to have a texture, representation of whatever it is we’re building.
And we’ll also define the depth of our tree because we’re essentially building a composite tree of objects.
So here I’ll make var B equals a new string builder like so I’ll pass string builder into print with
a value of zero because the default level is the top level.
So that’s going to be zero.
And then I’ll return SB dot two string like so and that’s that.
So when we print the object, let’s take a look at the kind of things we’re going to append to the string
builder.
So first of all, we’ll make a new string indicating the indentation.
So I’ll just use asterisks for this one and I’ll use the depth variable to indicate how deep we are
in the overall tree.
Actually, this should be a character, not a string.
Okay, so after this I will append the color, assuming that we do have a color because a group, for
example, doesn’t have a color, it doesn’t make sense to for the entire group to have a particular
color.
So here I’m going to append and I’m going to say if indeed there is a color.
So if there is no color, if the color is null or whitespace, then I’m going to return string empty
here.
Otherwise I’ll just put color here and I’ll put a space at the end of it.
So that’s the color.
And then I’m going to append the name.
So that’s going to be append name like so.
And after this I will append each one of the children.
If it’s a group, it has a bunch of children so we can append those.
So for each var child, let’s actually jump to the right location here.
So children, so for each var child and children, what I’m going to do is I’m going to call print recursively,
but I’m going to increment the depth.
So I’ll say child dot print SB comma depth plus one.
There we go.
So we now have some sort of printing mechanism for actually showing all the objects and we can actually
start building a composite drawing of some kind.
So var drawing equals new graphic object.
And here I’m going to give it a name.
So I’m going to say that the name is going to be my drawing, like, so drawing, and then I’ll add
a couple of objects to it.
So I’ll say drawing dot children, dot add.
And if you don’t like accessing the children directly, you can add helper methods such as add child,
for example.
That often is done in the real world.
So I’ll add a new square where the color is going to be red, for example.
And then in addition, let’s see how you can complete this statement.
There we go.
I’ll duplicate this and I’ll add, let’s say a circle circle with the color equal to yellow.
Now we’re going to take this drawing and we’re going to add a group to it.
So I’ll say var group equals new graphic object, just not giving it any particular properties, but
I’m going to add a couple of elements to it.
So I’m going to say group dot children, dot add and I’ll add a new circle.
Let’s have a blue circle.
Color equals blue, and I’ll also make a blue square as well.
So new square.
And then I’m going to add this group to the drawing.
So I’m going to say drawing dot children, dot, add the entire group and then we can print the whole
thing.
So right line and have the drawing in here.
So let’s take a look at what we get as we execute this.
All right.
So as you can see, we forgot a couple of end lines.
Let’s just add those briefly.
It’s all looking rather weird.
So this has to be an append line, I believe.
And let’s run this again.
Okay.
So we have my drawing on the top level.
We have one asterisk for Red Square and Yellow Square, and then we have a group and then we have two
asterisks because we’re one level deeper.
So this is a small illustration of how the composite design pattern looks in practice.
So you basically have an object consisting of objects, potentially objects of its own type.
We can also make these composite objects lazy in the sense that if you don’t need a list, then you
don’t get a list, you don’t even initialize it.
You don’t call the list constructor because we wrapped it in a lazy object.
And then of course, you can recursively iterate these structures and print out the results in this
case.
Play Stop Play Play Start Play information alert