terminal

codeando_simple

terminal

menu

terminal

search_module

guest@codeandosimple: ~/system/search $ grep -r "" .

Press [ENTER] to execute search

Status

Engine: Ready

Database: Online

Index: V2.1.0_LATEST

bash -- cat oop-associations.md
guest@codeandosimple: ~/blog/oop $ cat associations.md

Associations between Objects_

// "Have patience with all things, especially with yourself" - Saint Francis de Sales

One of the fundamental topics to understand in the world of Object-Oriented Programming and system modeling is ... how do objects relate to each other?  Associations between objects define how instances of one class can be linked to instances of another class.

In this chapter, we are going to look at the different types of associations, focusing on multiplicity, composition, and aggregation.

# Associations between Objects: The Fundamental Link

An association is a connection between two classes, indicating that instances of one class can know about or interact with instances of another class.

Associations are the foundation upon which more complex relationships are built.

In a UML class diagram, an association is represented as a line connecting two classes. Optionally, labels and arrows can be added to clarify the type of relationship.

# Multiplicity: Defining the Quantity

Multiplicity is a concept that defines how many instances of one class can be associated with an instance of another class.

It is a way to express the quantity and nature of connections between classes.

  • Notation: It is placed near the end of the association and can take several values like 1 (exactly one), 0..1 (none or one), * (zero or more), 1..* (one or more), etc.

  • Importance: Multiplicity helps in understanding possible interactions between objects, such as knowing if an object can exist without being associated with another.

# Composition: Strong and Exclusive Associations

Composition is a special type of association representing a "contains-a" or "part-of" relationship between objects. It indicates that an instance of one class is an essential part and intrinsically linked to another.

  • Characteristics: If the containing object (the whole) is destroyed, all its parts must also be destroyed. Composition is a strong and exclusive relationship.

  • UML Notation: It is represented by a line with a solid (black) diamond at the container's end.

# Aggregation: Associations with Independence

Aggregation is similar to composition in that it represents a "part-of" relationship, but it's less strict. In aggregation, the parts can exist independently of the whole.

  • Characteristics: If the containing object is destroyed, the parts can still exist.

  • UML Notation: It is represented by a line with a hollow (white) diamond at the container's end.

# Example: Order, Customer, and Product

To understand how objects link within a system and how concepts like association, multiplicity, composition, and aggregation apply in a real context, we are going to model a simple e-commerce system that includes Order, Customer, and Product.

System Description

  • Customer: Represents a person who places orders. A customer can place several orders.

  • Order: Represents an order placed by a customer. An order can include several products and is associated with exactly one customer.

  • Product: Represents the items that the customer can order. A product can be part of several orders.

Associations and Multiplicity

  • Customer to Order: A customer can place zero or many orders (multiplicity 0..*), but each order is associated with exactly one customer (multiplicity 1).

  • Order to Product: An order can contain one or many products (multiplicity 1..*) and a product can be part of zero or many orders (multiplicity 0..*).

Aggregation and Composition

  • Composition (Order and Customer): Although an order is strongly associated with a customer, it is not an essential part of a customer. A customer can exist without an order. Therefore, this relationship is not a composition.

  • Aggregation (Order and Product): An order contains products, but products can exist without being associated with an order. This is an aggregation, where the order is a whole and the products are the parts.

# UML Diagram

This is how we could represent these relationships in UML:

associations between objects

In this diagram:

  • Classes: Represented with rectangles divided into three parts for the class name, attributes, and methods.

  • Customer-Order Association: Represented by a line, the multiplicity "0..*" near Order indicates that a customer can place zero or many orders. The multiplicity "1" near Customer indicates that each order is associated with a single customer.

  • Order-Product Aggregation: Represented by a line that has a white diamond at the Order end. The multiplicity "1..*" near Product indicates that an order can contain one or many products. The multiplicity "0..*" near Order indicates that a product can be part of zero or many orders.

# Conclusions

Associations, multiplicities, compositions, and aggregations are fundamental tools for modeling how different objects and classes relate and work together.

We need to understand these tools because they are key when designing systems; it's a common language established in any work team.