Team LiB
Previous Section Next Section

Composition

Whenever we create a class, such as Student or Professor, in which one or more of the attributes are themselves handles on other objects, we are employing an OO technique known as composition. The number of levels to which objects can be conceptually bundled inside one another is endless, and so composition enables us to model very sophisticated real-world concepts. As it turns out, most "interesting" classes employ composition.

With composition, it may seem as though we're nesting objects one inside the other, as depicted in Figure 3-8.

Click To expand
Figure 3-8: Conceptual object "nesting"

Actual object nesting (i.e., declaring one class inside of another) is possible in some OO programming languages, and does indeed sometimes make sense: namely, if an object A doesn't need to have a life of its own from the standpoint of an OO application, and only exists for the purpose of serving enclosing object B.

However, we often encounter the situation—as with the sample Student and Professor classes—in which an object A needs to refer to an object B, object B needs to refer back to A, and both objects need to be able to respond to requests independently of each other as made by the application as a whole. In such a case, handles come to the rescue! In reality, we are not storing whole objects as attributes inside of other objects; rather, we are storing references to objects. When an attribute of an object A is defined in terms of an object reference B, the two objects exist separately in memory, and simply have a convenient way of finding one another whenever it's necessary for them to interact. Think of yourself as an object, and your cellular phone number as your reference. Other people—"objects"—can reach you to speak with you whenever they need to, even though they don't know where you're physically located, using your cell phone number.

Memory allocation using handles might look something like Figure 3-9 conceptually.

Click To expand
Figure 3-9: Objects exist separately in memory and maintain handles on one another.

With this approach, each object is allocated in memory only once; the Student object knows how to find and communicate with its advisor (Professor) object whenever it needs to through its handle/reference, and vice versa.

What do we gain by defining the Student's advisor attribute as a reference to a Professor object, instead of merely storing the name of the advisor as a string attribute of the Student object?

For one thing, we can ask the Professor object its name whenever we need it (through a technique that we'll discuss in Chapter 4). Why is this important? To avoid data redundancy and the potential for loss of data integrity.

Just as importantly, by maintaining a handle on the Professor object via the advisor attribute of Student, the Student object can also request other services of this Professor object via whatever methods are defined for the Professor class. A Student object may, for example, ask its advisor (Professor) object where the Professor's office is located, or what classes the Professor is teaching so that the Student can sign up for one of them.

Another advantage of using object handles from an implementation stand-point is that they also reduce memory overhead. Storing a reference to (aka memory address of) an object only requires 4 bytes (on 32-bit machines) or 8 bytes (on 64-bit machines) of memory, instead of however many bytes of storage the referenced object as a whole occupies in memory. If we were to have to make a copy of an entire object every place we needed to refer to it in our application, we could quickly exhaust the total memory available to our application.


Team LiB
Previous Section Next Section