link: Object Oriented Programming
Relations Between Objects
Overview
In software development, understanding the various types of relationships between objects is crucial for designing robust and maintainable systems. Beyond Inheritance and interface implementation, several other relationships define how objects interact and depend on each other within a system.
Abstract
This note explores the foundational relationships such as dependency, association, aggregation, and composition, each playing a unique role in object-oriented design and affecting how classes and objects are interconnected.
Content
Key Concepts
The relationships between objects in object-oriented programming are essential for defining how objects and classes interact. These relationships can be organized into several categories, each signifying different ways classes and objects can be associated:
Important
- Dependency: A change in one class affects another.
- Association: A general connection between classes where one class knows about and interacts with another.
- Aggregation: A specialized form of association where one class is made up of one or more instances of other classes.
- Composition: A stricter form of aggregation where the contained class’s lifecycle is controlled by the container class.
- Realization: This relationship occurs when a class implements an interface, thereby adhering to the contract the interface provides.
- Generalization: A relationship where a more specialized element (a derived class) inherits from a more general element (a base class). This is a form of inheritance.
Dependency
Dependency relationships
Content
Dependency is the most basic and the weakest type of relations between classes. In object-oriented programming (OOP), a dependency exists whenever one class uses another. This can be through direct instantiation, method calls, or through passing instances as parameters. Dependencies make one class reliant on another, often leading to a higher risk of bugs when changes are made:
Example
Here’s a practical C# example illustrating a basic dependency relationship:
csharp
In this code:
- The
Driver
class has a dependency on theCar
class because it uses theCar
object to perform its functions. The methodDrive
directly interacts withCar
, implying that any change inCar
’s interface would require changes inDriver
.Link to originalExample
This C# code snippet demonstrates a clear dependency where
Driver
relies onCar
. Such dependencies are vital to identify as they dictate the robustness and flexibility of your code structure.
Association
Association relationships
Content
Association in object-oriented design signifies a broader and more permanent relationship compared to simple dependencies:
Important
Association often represents links like fields in a class that refer to other objects. These links enable one object to use or interact with another object continuously, unlike a simple dependency which might only represent temporary use.
Example
Here’s a practical example to illustrate association and its contrast with dependency:
csharp
In this C# example, the
Professor
class has a propertyStudent
which exemplifies an association because it represents a permanent link to aStudent
object that any method of theProfessor
can use.Meanwhile, the
Teach
method’s use of theCourse
object to callGetKnowledge
shows a Dependency relationships. Changes inCourse
’s method could disrupt theProfessor
’s functionality, highlighting how dependencies are based on method use.The presence of both association and dependency in this example helps delineate the nuances between these two types of relationships.
Link to original
Aggregation
Aggregation relationships
Content
Aggregation allows one object to contain a group of objects but without exclusive ownership, meaning the contained objects can exist independently of the container, which is different from composition where the lifecycle of contained objects is managed by the container.
Important
Aggregation is used to represent relationships where the “part” (component) can exist separately from the “whole” (aggregate). This relationship is particularly useful in models where components need to be shared or reused across different parts of a system
Example
Here is a practical example to illustrate the concept of aggregation in C#:
In this C# example:
Link to original
- The
Department
class aggregatesEmployee
objects, representing a relationship where the department (whole) consists of employees (parts).- The
Employee
objects can exist independently of theDepartment
, indicating an aggregation, not composition.
Composition
Composition relationships
Content
Composition is a stringent form of aggregation in object-oriented programming that defines a “part-whole” relationship where parts cannot exist independently of the whole. This type of relationship is crucial for ensuring that objects are well-integrated within a system, enforcing encapsulation and enhancing the system’s robustness.
Composition is a specific kind of aggregation known for its strictness in part-whole relationships:
Important
Composition ensures that when the container (whole) is destroyed, its contents (parts) are also destroyed. This relationship is ideal for representing natural part-whole hierarchies in an application, such as graphical user interfaces or management systems where components must not exist without the whole.
Example
In this C# example:
Link to original
- The
Car
class is composed of anEngine
. TheCar
owns theEngine
and controls its lifecycle.- The
Engine
cannot exist independently of theCar
, demonstrating a composition relationship.- The methods
Start
andStop
inCar
delegate toEngine
, encapsulating its functionality within theCar
.
Summary
Cheat Sheet
Exploring how all these relationships interconnect provides insights into their practical applications and differences:
- Dependency: Suggests a unidirectional relationship where changes in one class (Class B) could affect another class (Class A).
- Association: Indicates a bidirectional relationship where each class knows about and can interact with the other.
- Aggregation: Implies a whole/part relationship without life dependency. Class A contains Class B, but B can exist independently of A.
- Composition: Represents a stronger whole/part relationship where the life of Class B is dependent on the life of Class A.
- Implementation and Inheritance: Both deal with how classes share interfaces and implementations, but inheritance implies a direct lineage.