您的位置:首页 > 其它

设计模式Before-after之桥接模式

2014-03-12 23:39 447 查看
before.cxx

#include <iostream>

class BlackCircle {
public:
void what(void) {
std::cout << "a black circle." << std::endl;
}
};

class BlackSquare {
public:
void what(void) {
std::cout << "a black square." << std::endl;
}
};

class WhiteCircle {
public:
void what(void) {
std::cout << "a white circle." << std::endl;
}
};

class WhiteSquare {
public:
void what(void) {
std::cout << "a white square." << std::endl;
}
};

int main(void) {
BlackCircle blackCircle;
blackCircle.what();

// BlackSquare blackSquare;
// blackSquare.what();

// WhiteCircle whiteCircle;
// whiteCircle.what();

// WhiteSquare whiteSquare;
// whiteSquare.what();
return 0;
}


after.cxx
#include <iostream>
#include "config.hxx"

class Color {
public:
virtual const char *getName(void) = 0;
};

class Black: public Color {
public:
const char *getName(void) {
return "black";
}
};

class White: public Color {
public:
const char *getName(void) {
return "white";
}
};

class Shape {
public:
virtual void what(void) = 0;
};

class Circle: public Shape {
public:
Circle(Color &color): color(color) {}
void what(void) {
std::cout << "a " << color.getName() << " circle" << std::endl;
}
public:
Color &color;
};

class Square: public Shape {
public:
Square(Color &color): color(color) {}
void what(void) {
std::cout << "a " << color.getName() << " square" << std::endl;
}
public:
Color &color;
};

int main(void) {
Shape &shape = _SHAPE_(_COLOR_());
shape.what();
return 0;
}


config.hxx
#define _COLOR_ Black

// #define _COLOR_ White

#define _SHAPE_ Circle

// #define _SHAPE_ Square
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: