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.

Supplement: C++ Code Examples<br />

Example 9-4 C++ Code Fragments: Rectangles Only<br />

void Rectangle::draw () {<br />

drawLine(_x1,_y1,_x2,_y1);<br />

drawLine(_x2,_y1,_x2,_y2);<br />

drawLine(_x2,_y2,_x1,_y2);<br />

drawLine(_x1,_y2,_x1,_y1);<br />

}<br />

void V1Rectangle::drawLine<br />

(double x1, double y1,<br />

double x2, double y2) {<br />

DP1.draw_a_line(x1,y1,x2,y2);<br />

}<br />

void V2Rectangle::drawLine<br />

(double x1, double y1,<br />

double x2, double y2) {<br />

DP2.drawline(x1,x2,y1,y2);<br />

}<br />

Example 9-5 C++ Code Fragments:<br />

Rectangles and Circles without Bridge<br />

class Shape {<br />

public: void draw ()=0;<br />

}<br />

class Rectangle : Shape {<br />

public:<br />

void draw();<br />

protected:<br />

void drawLine(<br />

double x1,y1, x2,y2)=0;<br />

}<br />

void Rectangle::draw () {<br />

drawLine(_x1,_y1,_x2,_y1);<br />

drawLine(_x2,_y1,_x2,_y2);<br />

drawLine(_x2,_y2,_x1,_y2);<br />

drawLine(_x1,_y2,_x1,_y1);<br />

}<br />

Chapter 9 The Bridge Pattern 157<br />

(continued)

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

Saved successfully!

Ooh no, something went wrong!