Composing With Classes
00:00
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 .x
and .y
position in space.
00:29
A class like this can make information easier to handle. It allows you to reference the values with labels like .x
and .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.
00:45
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 .points
.
01:00
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.
01:39
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 Shape
class.
01:50
So the way that this might work is that say you want to define a triangle
, and this will be a Shape
.
01:59
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.
02:12
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, 5
across, 5
up.
02:34
And then we’ll have another point, say, at 2
across, 4
up.
02:42
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 Point
instance.
03:02
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 Shape
object.
03:15
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 Point
objects.
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.
04:25
So you have a bottom_left
variable, bottom_right
variable, top_left
variable, top_right
variable. And these represent different points.
04:36
They outline a square because point bottom_left
is at 0, 0
; point bottom_right
is at 10
across, 0
up; top_left
, 0
across, 10
up; and top_right
is at 10, 10
.
04:48 So that would kind of outline a square.
04:54
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 Point
class.
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.