1. Computing & Technology

Discuss in my forum

A Decorator

Delphi OOP Part 14 - Chapter 27

By , About.com Guide

Materials written by John Barrow. Modifications by Zarko Gajic

Back to Chapter 26

Introduction

In chapters 11 and 13 we looked at ways of making the ‘same’ object behave differently under different circumstances by changing the steps of an algorithm, the Template Method pattern, or by changing the algorithm itself, the Strategy pattern, or by changing the behaviour of an entire object or set of objects, the Factory patterns. These are all ways of changing the core operations of a class.

In this chapter we look at situations where it is not necessary to change the core characteristics of an object at run time, but rather optionally to be able to add behaviour to the object’s existing behaviour. In OO parlance we want to decorate an object and so we’ll look at the Decorator pattern.

Background to the Decorator Pattern example

Joe's rather sleazy and cheesy Bioscope is a small-town cinema that shows full length features every Wednesday, Friday and Saturday. Joe's problem is that his attendance figures are falling steadily. In response he has introduced a Golden Oldies Club for senior citizens and a Silver Screen Loyalty Club for regular patrons. Members of these clubs receive tickets with additional customised annotations.

Members of either or both clubs receive individually customised tickets. Golden Oldies get a special welcoming message while Silver Screeners (members of the loyalty programme) get a special message of appreciation.

The Decorator Pattern

Joe’s Bioscope is a rather silly example, but it poses an interesting question. How does one change an object's behaviour in response to changing run time conditions? We have already looked at three approaches: the Template Method, where different subtypes implement the steps of a template in different ways, the Strategy, where a class can be associated with different algorithms or sets of rules, and the Factories, where entirely different objects are produced.

Here we'll look at a fourth way of varying an object's run time behaviour, through use of the Decorator pattern. Whereas the previous patterns change the inner workings of an object or the object itself, the Decorator adds behaviour around a fixed core object. Because of this characteristic, a Decorator is sometimes also called a Wrapper. (This is rather confusing since Wrapper is also an alternative name for the Adapter pattern. We avoid using the word Wrapper in these notes.)

Read the full chapter in the provided PDF...

Chapter Contents

  • Background to the Decorator Pattern example
  • Example 14.1 A simple solution
  • The Decorator Pattern
  • Example 14.2 Applying the Decorator pattern
  • Ex 14.2 step 1 Coding the Decorator
  • Ex 14.2 step 2 Using the Decorator
  • Ex 14.2 step 3 Another way to add the decorations
  • Ex 14.2 step 4 Visualising the iterations
  • Decoration through aggregation or inheritance?
  • Pattern 14.1 The Decorator Pattern
  • Recapitulation

Chapter Download

Here's the full text of the Delphi OOP part 14 available as PDF, along with the Delphi source code examples: This end the Delphi OOP Course.

©2012 About.com. All rights reserved.

A part of The New York Times Company.