设计模式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
#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
相关文章推荐
- 设计模式Before-after之组合模式
- 设计模式Before-after之单例模式
- 设计模式Before-after之装饰模式
- 设计模式Before-after之简单工厂模式
- 设计模式Before-after之适配器模式
- 设计模式Before-after之享元模式
- 设计模式Before-after之代理模式
- 设计模式Before-after之工厂方法模式
- 设计模式笔记(8)---桥接模式(结构型)
- 设计模式 桥接模式
- 设计模式:22 手机软件何时统一_桥接模式
- 设计模式之桥接模式——链接两地的交通枢纽
- 设计模式:桥接模式(学习笔记)
- Java设计模式 - 桥接模式与策略模式的区别
- 设计模式——桥接模式
- 设计模式之桥接模式
- 设计模式之桥接模式
- iOS 设计模式 - 桥接模式
- 小话设计模式(七)桥接模式
- 设计模式之桥接模式