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.

22O Part IV • Putting It All Together: Thinking in <strong>Patterns</strong><br />

The context of the<br />

Abstract Factory<br />

the patterns themselves allow me to anticipate possible variation<br />

because I have divided my system into well-behaved classes,<br />

thereby making changes easier to accommodate. <strong>Design</strong> patterns<br />

help me see where variations may occur, not so much which partic -<br />

ular variations will occur. The well-defined interface I use to contain<br />

my current variations often contains the impacts of new<br />

requirements just as well.<br />

The Abstract Factory is another good example of designing by context.<br />

I may understand early on that a factory object of some sort<br />

will be used to coordinate the instantiation of families (or sets) of<br />

objects. However, there are many different ways to implement it as<br />

follows.<br />

For... The Abstract Factory Says ...<br />

Using derived classes<br />

Using a single object with<br />

switches<br />

Using a configuration file<br />

with switches<br />

Using a configuration file<br />

with RTTI<br />

(runtime-type-identificati<br />

on)<br />

The classic Abstract Factory implementation<br />

requires me to implement a derivation for<br />

each set that I need. This is a little cumbersome<br />

but has the advantage of allowing me<br />

to add new classes without changing any of<br />

my existing classes.<br />

If I am willing to change the Abstract Factory<br />

class as needed, I can simply have one<br />

object that contains all of the rules. While<br />

this doesn't follow the open-closed principle,<br />

it does contain all of my rules in one place<br />

and is not hard to maintain.<br />

This is more flexible than the prior case, but<br />

still requires modifying the code at times.<br />

RTTI includes a way of instantiating objects<br />

based on the name of the object placed in a<br />

string. Implementations of this sort have<br />

great flexibility in that new classes and new<br />

combinations can be added without having<br />

to change any code.

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

Saved successfully!

Ooh no, something went wrong!