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.

18O Part III • <strong>Design</strong> <strong>Patterns</strong><br />

Supplement: C++ Code Examples<br />

Example 1O-4 C++ Code Fragments: A Switch<br />

to Control Which Driver to Use<br />

// C++ CODE FRAGMENT<br />

// class ApControl<br />

void ApControl::doDraw (} {<br />

. . .<br />

switch (RESOLUTION)<br />

{ case LOW:<br />

// use Irdd<br />

case HIGH: //<br />

use hrdd<br />

}<br />

}<br />

void ApControl::doPrint () {<br />

. . .<br />

switch (RESOLUTION)<br />

{ case LOW:<br />

// use lrpd<br />

case HIGH:<br />

// use hrpd } }<br />

Example 1O-5 C++ Code Fragments: Using<br />

Polymorphism to Solve the Problem<br />

// C++ CODE FRAGMENT<br />

// class ApControl<br />

void ApControl::doDraw () {<br />

myDisplayDriver->draw();<br />

}<br />

void ApControl::doPrint () {<br />

myPrintDriver->print();<br />

}

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

Saved successfully!

Ooh no, something went wrong!