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.

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

have handled the variation in tax rules by using inheritance to<br />

derive a new class with the new rule.<br />

Take a different How could I approach this differently? Following the rules I stated<br />

approach above: attempt to "consider what should be variable in your design"<br />

. . . and "encapsulate the concept that varies." 5<br />

Step 1: Find<br />

what varies and<br />

encapsulate it<br />

Following this two-step approach, I should do the following:<br />

1. Find what varies and encapsulate it in a class of its own.<br />

2. Contain this class in another class.<br />

In this example, I have already identified that the tax rules are<br />

varying. To encapsulate this would mean creating an abstract class<br />

that defines how to accomplish the task conceptually, and then<br />

derive concrete classes for each of the variations. In other words, I<br />

could create a CalcTax object that defines the interface to accomplish<br />

this task. I could then derive the specific versions needed. I<br />

show this, in Figure 14-3.<br />

Figure 14-3 Encapsulating tax rules.<br />

5. ibid, p. 29.

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

Saved successfully!

Ooh no, something went wrong!