Supplement: C++ Code Examples

Example 9-4. C++ Code Fragments: Rectangles Only
void Rectangle::draw () {
   drawLine(_x1,_y1,_x2,_y1);
   drawLine(_x2,_y1,_x2,_y2);
   drawLine(_x2,_y2,_x1,_y2);
   drawLine(_x1,_y2,_x1,_y1);
}

void V1Rectangle::drawLine
   (double x1, double y1,
    double x2, double y2) {
    DP1.draw_a_line(x1,y1,x2,y2);
}

void V2Rectangle::drawLine
   (double x1, double y1,
    double x2, double y2) {
    DP2.drawline(x1,x2,y1,y2);
}
Example 9-5. C++ Code Fragments: Rectangles and Circles without Bridge
 class Shape { public: void draw ()=0; } class Rectangle : Shape { public: void draw(); protected: void drawLine( double x1,y1, x2,y2)=0; } void Rectangle::draw () { drawLine(_x1,_y1,_x2,_y1); drawLine(_x2,_y1,_x2,_y2); drawLine(_x2,_y2,_x1,_y2); drawLine(_x1,_y2,_x1,_y1); ...

Get Design Patterns Explained: A New Perspective on Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.