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

Indirection - Delphi OOP Part 8 - Chapter 18

By Zarko Gajic, About.com

Pattern 8.1 The Law of Demeter

It is possible for an object to follow a public link in a second object to access a third object. Although this provides a very easily implemented form of inter-object communication, it leads to chained links and creates a highly coupled program where objects become dependent on the internal structure of a large number of other objects. Any change is likely to affect many classes, and the testing and maintenance effort is likely to be high.

Therefore,classes should communicate only with their neighbours and should not talk to strangers.

ObjectB is considered a neighbour of ObjectA if:

  • it is the same object (self),
  • ObjectA carries a reference to ObjectB as one of its data fields (ie an association exists),
  • ObjectB is passed as a parameter to a method of ObjectA,
  • ObjectA creates ObjectB,
  • ObjectB is a global variable.
If ObjectA requires a service provided by ObjectC, which is not a neighbour, it is generally preferable for that communication to be mediated through an intermediate object that is already a neighbour of both ObjectA and ObjectC. (See pattern 8.2, Delegation, below.) The Law of Demeter is applied slightly differently by different authors.

It is worth quoting part of Rumbaugh et al’s (1991, 285–286) summary of the Law of Demeter: ‘A method must be able to traverse links to obtain its neighbours and must be able to call operations on them, but it should not traverse a second link from the neighbour to a third class because the second link is not directly visible to it. Instead, call an operation on the neighbour object to traverse the operation; if the association network changes, the operation method can be rewritten without changing the call.’

This advice sums up the central theme of this chapter. In this chapter we have termed traversing a second link to a third class ‘chaining’ and in examples 8.2 and 8.4 have looked at the problems of chaining. Calling an operation on the neighbour object we have called ‘delegation’, and examples 8.3 and 8.5 discuss why delegation is preferable to chaining.

In summary, an important goal of the Law of Demeter is that any method or class should have direct interactions only with neighbouring objects in order to keep the coupling within the system low and to make the program easier to understand and change. A cost is that it introduces a programming overhead to create delegation methods when working through intermediate objects.

Pattern 8.2 Delegation

An existing class, ClassA, provides an operation you need for a new class, ClassB, but the ‘ClassB IsA ClassA’ relationship does not hold. It is therefore not appropriate to derive ClassB from ClassA. You also do not want to reimplement the operation in ClassB since it already exists in ClassA.

Therefore, create a method in ClassB that delegates the operation to the existing class, ClassA. This means that ClassB receives the message to perform the operation and then explicitly delegates this operation to ClassA.

If, instead of using delegation, ClassB was made a subclass of ClassA, all of ClassA’s operations would be exposed. With delegation, ClassB must specifically forward the operation to ClassA, and undesired operations are not available.

The name of the operation in ClassB can differ from that in ClassA. The delegating method in ClassB can participate in polymorphic substitution within ClassB’s inheritance hierarchy. In contrast to using chaining, the increased encapsulation and reduced coupling gained from delegation carries the cost of implementing each required delegating method.

Part 8 Homework Problems and PDFs Download

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 8 of the Delphi OOP Course.

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Indirection - Delphi OOP Part 8 - Chapter 18

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

All rights reserved.