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.

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

Decorators in testing I have used the Decorator to wrap precondition and postcondition<br />

tests on an object to be tested with nice results. During test, the first<br />

object in the chain can do an extensive test of preconditions prior to<br />

calling its trailing object. Immediately after the trailing object call,<br />

the same object calls an extensive test of postconditions. If I have<br />

different tests I want to run at different times, I can keep each test in<br />

a different Decorator and then chain them together according to the<br />

battery of tests I want to run.<br />

Summary<br />

In this chapter The Decorator pattern is a way to add additional function(s) to an<br />

existing function dynamically. In practice, it requires building a<br />

chain of objects that give the desired behaviors. The first object in<br />

this chain is called by a Client that had nothing to do with the<br />

building of it. By keeping the creation of the chain independent<br />

from its use, the Client object is not affected by new requirements<br />

to add functionality.<br />

Supplement: C++ Code Examples<br />

Example 15-2 C++ Code Fragments<br />

class SalesTicket : public Component<br />

{ public:<br />

void prtlicket();<br />

}<br />

SalesTicket::prtTicket() {<br />

// sales ticket printing code here<br />

}<br />

class Decorator : public Component<br />

{ public:<br />

virtual void prtTicket();<br />

Decorator( Component *myC);<br />

private:<br />

Component *myComp; }<br />

(continued)

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

Saved successfully!

Ooh no, something went wrong!