Skip to content

Decomposition

Estimated time to read: 4 minutes

Overview

Decomposition is taking a whole thing and dividing it up into different parts. Or, taking a collection of separate parts with different functionalities and combining them together to form a whole.

Decomposition allows you to further break down problems into pieces that are easier to understand and solve.

Relationships

There are three kinds of relationships found within Decomposition.

  • Association
  • Aggregation
  • Composition

Association

Association is 'some' relationship. This means that there is a loose relationship between two objects. These objects may interact with each other for some time.

As a general rule, if one object can exist without the other, then it is an association relationship.

Example

Screenshot 2022-07-13 at 20.35.49.png

  • A straight line between two UML objects shows that there is an association between them.
  • The o..* in the example above means that there are 0 or more of the one class being associated with the other.

Association Code Example

public class Wine {
 public void pair(Food food){
  execute.pair(food);
 }
}

Aggregation

Aggregation is a 'has-a' relationship where a whole has parts that belong to it. There may be a sharing of parts among the wholes in this relationship.

The 'has-a' relationship from a while to the parts is considered weak. What this means is although parts can belong to the wholes, they can also exist independently.

As a general rule, aggregated objects can exist without each other, but they are also reliant on each other to operate correctly.

Aggregation Example

Screenshot 2022-07-13 at 20.42.15.png

  • The empty diamond, against the Airliner, is the symbol for aggregation.
  • The empty diamond denotes which object is considered the whole in the relationship.
  • The o..* in the example above means that there are 0 or more of the one class being associated with the other.

Aggregation Code Example

public class PetStore{
 private ArrayList<Pet> pets;

 public PetStore(){
  pets = new ArrayList<Pet>();
 }

 public void add(Pet pet){...}
}

Composition

Composition is an exclusive containment of parts, otherwise known as a strong 'has-a' relationship.

This means that the whole cannot exist without its parts. If it loses any of its parts, the whole ceases to exist.

If the whole is destroyed, then all of its parts are destroyed too.

Usually, you can only access the parts through its whole. Contained parts are exclusive to the whole.

As a general rule, objects with a composition relationship can not exist without each other. They are fully dependant on each other.

Composition Example

Screenshot 2022-07-13 at 20.50.40.png

  • The filled in diamond means that the 'house' object is the whole in the relationship
  • The filled in diamond means that it is a strong 'has-a' relationship, a composition
  • The 1..* above shows that the 'house' object has 1 or more room object relationships

Composition Code Example

public class Employee{
  private Salary salary;

  public Employee(Salary employeeSalary){
    this.salary = employeeSalary;
  }
}

Lifetimes, Relations and Sharing

One issue in decomposition involves the lifetimes of the whole object, and the part objects, and how they could relate. Lifetimes might be closely related.

For example, the refrigerator and its freezer have the same lifetime. One cannot exist by itself without the other. If you dispose of the refrigerator, you would dispose of the freezer as well.

But lifetime can also not be so related. The refrigerator and food items have different lifetimes. Either can exist independently. Let's take a look at the lifetimes of parts in an example.

Consider the lifetime of a car, and name one part of a car that has a closely related lifetime, and one part that is not. As possible responses, a closely related lifetime would be the frame, and not closely related would be the tires. You can have whole things contain parts that are shared among them at the same time. How can this relationship arise?

Consider a person who has a daughter in one family, but also a spouse in another family. The two families are regarded as separate wholes, but they simultaneously share the same part. However, sometimes sharing is not possible or intended. For example, a food item in a refrigerator cannot at the same time also be inside an oven.

Overall, decomposition helps you to break down a problem into smaller pieces. A complicated whole thing can be composed out of constituent, separate, simpler parts. Important issues to understand are how the parts relate to the whole, such as fixed or dynamic number, their lifetimes, and whether there is sharing.

Multiplicity

This association relationship indicates that (at least) one of the two related classes make reference to the other.

This relationship is usually described as "A has a B" (a mother cat has kittens, kittens have a mother cat).

The UML representation of an association is a line connecting the two associated classes.

At each end of the line there is optional notation. For example, we can indicate, using an arrowhead that the pointy end is visible from the arrow tail.

We can indicate ownership by the placement of a ball, the role the elements of that end play by supplying a name for the role, and the multiplicity of instances of that entity (the range of number of objects that participate in the association from the perspective of the other end).

Instance Meaning
0 No Instances
0..1 No instances, or one instance
1 Exactly one instance
1..1 Exactly one instance
0..* Zero or more instances
* Zero or more instances
1..* One or more instances