Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

Association and Composition - Delphi OOP Part 9 - Chapter 21

By Zarko Gajic, About.com

Comparing association and composition

The focus with association is on the cooperation between objects, while the focus with composition is on encapsulation. If we look at the examples in this chapter, and give a voice to TSale, we might have the following.

For example 9.1 (association), TSale might say: ‘We’ll share responsibility for storing this data. I’ll take responsibility for storing the amount of the sale, and I’ll refer to TCustomer for any of the customer details.’

For example 9.2 (composition), TSale might say: ‘I’ll take complete responsibility for all the data. I’ll store the amount directly and I’ll create my own instance of TCustomer to store all the customer data.’

Association

  • A cooperation mechanism using delegation to distribute responsibility among associated objects.
  • A relationship of equality between associated but independent objects.
  • The lifetime of associates is not necessarily dependent on each other.
  • A public relationship, with the participating objects free to interact with other objects.
Composition
  • A reuse mechanism with delegation by the composed object to the encapsulated constituents.
  • A dependence relationship between constituents and composed object.
  • An ownership relationship, with the lifetime of the composed object bounding the lifetime of its constituents.
  • A private relationship, with outside objects unaware of the existence of the constituent objects.

Contrasting inheritance and linking / delegating

Both inheritance and linking can be used to provide reuse when building a new class. Each also has a different use: inheritance can be used to provide polymorphism, while linking is essential for objects to cooperate.

Part Download and Homework Problems

There are a dozen of homework problems included in the Part's PDF. Make sure you solve all of them.

This is the end of Part 9 of the Delphi OOP Course.

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

More from About.com

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Association and Composition - Delphi OOP Part 9 - Chapter 21

©2008 About.com, a part of The New York Times Company.

All rights reserved.