Composing With Classes
How do you combine classes together into systems? The first way you’re going to look at is with composition. Take a look at this class. It’s a
Point class that represents a point in two-dimensional space using the Cartesian coordinate system. You have your class definition, you have your constructor method, and you’re assigning some attributes in the constructor, Your
.y position in space.
A class like this can make information easier to handle. It allows you to reference the values with labels like
.y and can be better than having the X and Y coordinates in a list, for instance, where there is no immediate indication as to what they represent.
A way you might use this in composition is if you were building shapes. Take this
Shape class, for instance. In the constructor method, you are setting an attribute called
And in that, you’re setting the points that were passed in in the constructor. You can imagine that the argument would be passed in as a list. So then as an attribute of the
Shape instance, you’d have
.points, which would be a list of points.
01:20 So within this class, its attribute is a list of instances of a different class. Each class is independent, but one class relies on the other class for its attributes.
01:34 This is an example of composition.
Loading up these two classes in IDLE—got my editor on the right-hand side and the shell on the left-hand side—you can see the
Point class and the
So the way that this might work is that say you want to define a
triangle, and this will be a
This expression calls the
Shape() constructor, and the
Shape constructor needs a
points argument. And since this is going to be various points, you’re going to make that a list.
We’re going to split this up onto various lines so that it looks neater. And each item in the list will be a
Point. Let’s start off with a point at the origin, and then we’ll have another point at, say,
And then we’ll have another point, say, at
So here everything’s happening in this one expression. You are instantiating a
Shape, and the
Shape() constructor needs one argument, which is the
points. We’re starting a list, and then within that list, each element of that list is an instantiation of a
We can run this—we’ll first save with Control + S and then F5—and that ran without any errors. So now if we can look at
Triangle, you can see that that’s a
And if you look at the
triangle.points, you can see that you have a list and one
Point object, two
Point objects, and three
03:29 And that is an example of composition. You’re using one class as a way to build the attributes of other classes. This doesn’t have to be limited to one class being an attribute of another class that takes it in.
03:45 You can have many low-level classes that stand quite independently, and then you can have one or more classes that group them together into higher-level classes. As the name suggests, composition is something of a creative activity.
04:00 It’s up to you how you want to compose things together and where you draw the boundaries between different concepts, models, and ideas.
04:13 So as you’ve seen, you can define many different variables. Here’s another way of looking at the same sort of process. Here you’ve got a bunch of variables that define points.
So you have a
top_right variable. And these represent different points.
They outline a square because point
bottom_left is at
0, 0; point
bottom_right is at
10 up; and
top_right is at
04:48 So that would kind of outline a square.
Then you can instantiate a new
Shape object and then pass in, in a list, the four variables that you just defined. Then you’ll see that
square is a
Shape, is an instantiated
Shape, and the
.points attribute of the
square contain four instances of the
05:18 And that’s composition, an extremely powerful and creative way to combine classes together to form more complex systems.
Become a Member to join the conversation.