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.

Intent<br />

Problem<br />

Solution<br />

Participants and<br />

Collaborators<br />

Consequences<br />

Implementation<br />

GoF Reference<br />

Chapter 15 • The Decorator Pattern 251<br />

The Decorator Pattern: Key Features<br />

Attach additional responsibilities to an object dynamically.<br />

The object that you want to use does the basic functions you require.<br />

However, you may need to add some additional functionality to the object,<br />

occurring before or after the object's base functionality. Note that the Java<br />

foundation classes use the Decorator pattern extensively for I/O handling.<br />

Allows for extending the functionality of an object without resorting to subclassing.<br />

The ConcreteComponent is the class having function added to it by the<br />

Decorators. Sometimes classes derived from ConcreteComponent<br />

are used to provide the core functionality, in which case<br />

Concrete-Component is no longer concrete, but rather abstract. The<br />

Component defines the interface for all of these classes to use.<br />

Functionality that is to be added resides in small objects. The advantage is<br />

the ability to dynamically add this function before or after the functionality in<br />

the ConcreteComponent. Note: While a decorator may add its<br />

functionality before or after that which it decorates, the chain of instantiation<br />

always ends with the ConcreteComponent.<br />

Create an abstract class that represents both the original class and the new<br />

functions to be added to the class. In the decorators, place the new function<br />

calls before or after the trailing calls to get the correct order.<br />

Pages 175-184.<br />

Figure 15-7 Standard, simplified view of the Decorator pattern.

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

Saved successfully!

Ooh no, something went wrong!