Custom Development

Aggregation, Composition & Delegation

Mohamed Farag

Summa gave me the opportunity to attend a 7 hour lecture by Ken Pugh about design patterns. At this lecture, I had a good chance to strengthen my design pattern knowledge and find attractive solutions to related questions. For a long time, the programmatic representation of aggregation, composition and delegation had confused me. I knew that I solved this problem when I attended Ken Pugh’s lecture. Object lifetime and eligibility for being class attribute explained the difference between the representations. Three main entities are explained in the following points:

Composition:

In composition, lifetime of an object is completely controlled by the lifetime of another object. The controlled object is referenced as class attribute in the controller.

Composition

Figure 1.0

Composition-1

In complex systems, the factory pattern can handle object creation process. Abstract factory can be used in more complex systems to handle this process.

Aggregation:

In aggregation, the lifetime of objects is independent from each other. However, aggregation is the minimum level of class attribute association.

aggregation

Figure 2.0

The previous diagram can be coded as the following:

aggregation-1

 

Delegation:

In delegation, life time of objects is totally independent from each other. The relationship between objects is temporary and injection is limited to function lifetime. Delegation is not represented as class attribute in any way!

Delegation

Figure 3.0

Delegation-1

Some people represent delegation relationship in terms of class attributes for referential purposes but this is not the ideal representation of delegation.

Summary:

The following table summarizes the main points that were discussed:

 

Affects Object Lifetime?

Class Attribute?

Composition

Yes

Yes

Aggregation

No

Yes

Delegation

No

No

Table 1.0

Mohamed Farag
ABOUT THE AUTHOR

Technical Consultant