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.

Example 15-1 Java Code Fragment: Decorator (continued)<br />

Chapter 15 • The Decorator Pattern 247<br />

Component myST;<br />

// Get chain of Decorators and SalesTicket built by<br />

// another object that knows the rules to use.<br />

// This may be done in constructor instead of<br />

// each time this is called.<br />

myST= Configuration.getSalesTicket()<br />

// Print Ticket with headers and footers as needed<br />

myST.prtTicket 0 ; } }<br />

If I want the sales ticket to look like:<br />

HEADER 1<br />

SALES TICKET<br />

FOOTER 1<br />

Then Configuration.getsalesTicket returns<br />

return( new Headerl( new Footerl( new SalesTicket())};<br />

This creates a Headerl object trailed by a Footerl object trailed by a<br />

SalesTicket object.<br />

If I want the sales ticket to look like:<br />

HEADER 1<br />

HEADER 2 SALES<br />

TICKET FOOTER 1<br />

Then Configuration.getsalesTicket returns<br />

return( new Headerl( new Header2 (new Footerl(<br />

new SalesTicket ())});<br />

What happens in<br />

the code

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

Saved successfully!

Ooh no, something went wrong!