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.

Chapter 1O • The Abstract Factory Pattern 165<br />

A high-resolution family— HRDD and HRPD, those drivers that put<br />

high demands on the system<br />

My first attempt might be to use a switch to control the selection of Alternative 1: use a<br />

driver, as shown in Example 10-1. switch to select the<br />

driver<br />

Example 1O-1 Java Code Fragments: A<br />

Switch to Control Which Driver to Use<br />

// JAVA CODE FRAGMENT<br />

class ApControl {<br />

}<br />

}<br />

}<br />

• • •<br />

void doDraw () {<br />

switch (RESOLUTION){<br />

}<br />

case LOW:<br />

// use lrdd<br />

case HIGH:<br />

// use hrdd<br />

void doPrint () {<br />

• • •<br />

switch (RESOLUTION) {<br />

case LOW:<br />

// use lrpd<br />

case HIGH:<br />

// use hrpd<br />

}<br />

While this does work, it presents problems. The rules for determining<br />

which driver to use are intermixed with the actual use of the driver.<br />

There are problems both with coupling and with cohesion:<br />

• Tight coupling— If I change the rule on the resolution (say, I need to<br />

add a MIDDLE value), I must change the code in two places that<br />

are otherwise not related.<br />

… but there are<br />

problems with<br />

coupling and<br />

cohesion

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

Saved successfully!

Ooh no, something went wrong!