23.07.2012 Views

Design Patterns Explained

Design Patterns Explained

Design Patterns Explained

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

244 Part V • Handling Variations with <strong>Design</strong> <strong>Patterns</strong><br />

The Decorator<br />

pattern is a chain of<br />

objects<br />

In this case<br />

Figure 15-3 The Decorator chain.<br />

The class diagram of the Decorator pattern in Figure 15-4 implies the<br />

chain of objects shown in Figure 15-3. Each chain starts with a Component<br />

(a ConcreteComponent or a Decorator). Each Decorator<br />

is followed either by another Decorator or by the original ConcreteComponent.<br />

A ConcreteComponent always ends the chain.<br />

Figure 15-4 The Decorator pattern class diagram.<br />

For example, in Figure 15-4, ConcreteDecoratorB performs its<br />

Operation and then calls the Operation method in Decorator. This<br />

calls ConcreteDecoratorB's trailing Component's Operation.<br />

Applying the Decorator<br />

Pattern to the Case Study<br />

In the case study, the SalesTicket is the ConcreteComponent.<br />

The concrete decorators are the headers and footers. Figure 15-5<br />

shows the application of the Decorator pattern to the case study.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!