Understanding Object Relationships
Distinguish between composition, aggregation, association, and dependency.
What Are Object Relationships?
Object relationships describe how different objects in a program are connected to and interact with each other. C++ supports several types of relationships -- including composition, aggregation, association, and dependency -- each modeling a different real-world connection between objects.
Life is full of recurring patterns, relationships, and hierarchies between objects. By exploring and understanding these, we can gain insight into how real-life objects behave, enhancing our understanding of those objects.
For example, imagine you're walking through a garden and see a red object attached to a woody stem. You'd probably recognize that the red thing is a fruit, and the woody stem is part of a bush. Even though you'd never seen this particular type of bush before, you'd know that the green parts are leaves, absorbing sunlight. You'd know that the fruit helps the bush propagate itself. You'd also know that if you destroyed the bush, the fruit would die too.
How can you know all this without ever encountering this specific bush before? You know this because you understand the abstract concept of plants and recognize that this bush is an instantiation of that abstraction. You know that most plants are composed (in part) of leaves, and some produce fruit. You know that the leaves interact with sunlight (even if you don't know how, exactly), and that the fruit's existence depends on the plant. Because you know all these things about plants in general, you can infer much about this specific bush.
Similarly, programming is full of recurring patterns, relationships and hierarchies. Particularly when it comes to programming objects, the same patterns that govern real-life objects are applicable to the programming objects we create ourselves. By examining these in more detail, we can better understand how to improve code reusability and write classes that are more extensible.
In previous chapters, we've explored some ideas around recurring patterns: we've created loops and functions to allow us to perform a particular task many times. Additionally, we've created our own enums, structs, and classes to allow us to instantiate objects of a given type.
We've also explored some primitive forms of hierarchy, such as arrays (which allow us to group elements into a larger structure) and recursion, where a function calls a derivative version of itself.
However, we haven't yet focused much on the relationship between objects, particularly as it relates to programming.
Relationships between objects
There are many different kinds of relationships two objects may have in real-life, and we use specific "relation type" words to describe these relationships. For example: a triangle "is-a" shape. A building "has-a" door. A software developer "uses-a" computer. A tree "depends-on" water for survival. A student is a "member-of" a university. And your hand exists as "part-of" you (at least, we can reasonably assume so if you've gotten this far).
All of these relation types have useful analogies in C++.
In this chapter, we'll explore the nuances of the relation types "part-of", "has-a", "uses-a", "depends-on", and "member-of", and show how they can be useful in the context of C++ classes. We'll also explore a couple of related topics that don't fit nicely anywhere else.
Then we'll devote the following two chapters to exploring "is-a" relationships, via C++'s inheritance model and virtual functions.
Alright, enough context setting. Let's get to it.
Summary
Real-world patterns: Objects in both real life and programming exhibit recurring patterns, relationships, and hierarchies. Understanding these helps us model complex systems effectively.
Relationship types: Objects can have various relationships described by specific terms: "is-a" (inheritance), "has-a" (composition/aggregation), "uses-a" (association), "depends-on" (dependency), "member-of" (membership), and "part-of" (composition).
Object composition: The process of building complex objects from simpler ones. Common in both real life (computers built from CPUs, memory, etc.) and programming (classes with member variables).
Upcoming coverage: This chapter explores "part-of" (composition), "has-a" (aggregation), "uses-a" (association), "depends-on" (dependency), and "member-of" relationships in C++. Subsequent chapters cover "is-a" relationships through inheritance and virtual functions.
Understanding these relationship types allows us to write more maintainable, reusable, and extensible code by properly modeling how objects interact and depend on each other.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Understanding Object Relationships - Quiz
Test your understanding of the lesson.
Practice Exercises
Model Different Object Relationships
Create a simple system that demonstrates different types of object relationships: composition (part-of), aggregation (has-a), and association (uses-a). Model a university system with departments, professors, and courses.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!