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: Well share responsibility for storing this data. Ill take responsibility for storing the amount of the sale, and Ill refer to TCustomer for any of the customer details.
For example 9.2 (composition), TSale might say: Ill take complete responsibility for all the data. Ill store the amount directly and Ill 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.
- 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.

