23.07.2012 Views

Design Patterns Explained

Design Patterns Explained

Design Patterns Explained

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

The Factory Method Pattern: Key Features<br />

Intent Define an interface for creating an object, but let subclasses decide<br />

which class to instantiate. Defer instantiation to subclasses.<br />

Problem A class needs to instantiate a derivation of another class, but doesn't<br />

know which one. Factory Method allows a derived class to make this<br />

decision.<br />

Solution A derived class makes the decision on which class to instantiate and<br />

how to instantiate it.<br />

Participants and Product is the interface for the type of object that the Factory Method<br />

Collaborators creates. Creator is the interface that defines the Factory Method.<br />

Consequences Clients will need to subclass the Creator class to make a particu-<br />

lar ConcreteProduct.<br />

Implementation Use a method in the abstract class that is abstract (pure virtual in<br />

C++). The abstract class' code refers to this method when it needs to<br />

instantiate a contained object but does not know which particular<br />

object it needs.<br />

GoF Reference Pages 107-116.<br />

Figure 19-2 Standard, simplified view of the Factory Method pattern.

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

Saved successfully!

Ooh no, something went wrong!