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.

Chapter 13 • The Principles and Strategies of <strong>Design</strong> <strong>Patterns</strong> 223<br />

The Principle of Containing Variation<br />

Several people have remarked about a certain similarity in all of the<br />

designs they have seen me create: my inheritance hierarchies rarely<br />

go more than two levels of classes deep. Those that do typically fit<br />

into a design pattern structure that requires two levels as a base of<br />

the abstract classes (the Decorator pattern, discussed in Chapter 15,<br />

is an example that uses three levels).<br />

The reason for this is that one of my design goals is never to have a<br />

class contain two things that are varying that are somehow coupled<br />

to each other. The patterns I have described so far do illustrate different<br />

ways of containing variation effectively.<br />

•<br />

The Bridge pattern is an excellent example of contained variation.<br />

The implementations present in the Bridge pattern are all different<br />

but are accessed through a common interface. New implementations<br />

can be accommodated by implementing them within this<br />

interface.<br />

The Abstract Factory contains the variation of which sets or families<br />

of objects can be instantiated. There are many different ways of<br />

implementing this pattern. It is useful to note that even if one<br />

implementation is initially chosen and then it is determined<br />

another way would have been better, the implementation can be<br />

changed without affecting any other part of the system (since the<br />

interface for the factory does not change, only the way it is imple -<br />

mented). Thus, the notion of the Abstract Factory itself (implementing<br />

to an interface) hides all of the variations of how to create<br />

the objects.<br />

The Adapter pattern is a tool to be used to take disparate objects<br />

and give them a common interface. This is often needed now that I<br />

am designing to interfaces as called for in many patterns.<br />

A note on my designs<br />

Containing<br />

variation in the<br />

Bridge pattern<br />

Containing<br />

variation in the<br />

Abstract Factory<br />

Containing variation<br />

in the Adapter<br />

pattern

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

Saved successfully!

Ooh no, something went wrong!