dart_in_action/19_design_pattern_bridge.dart

66 lines
1.6 KiB
Dart
Raw Permalink Normal View History

2019-03-27 10:55:27 +08:00
/**
Bridge
使
使
*/
main(List<String> args) {
Shape redCircle = new Circle(100, 100, 10, new RedCircle());
Shape greenCircle = new Circle(100, 100, 10, new GreenCircle());
redCircle.draw();
greenCircle.draw();
}
//////////////////////////////////////////////////////////////////
///
/// 创建桥接实现接口
///
abstract class DrawAPI {
void drawCircle(int radius, int x, int y);
}
///
/// 创建实现了 DrawAPI 接口的实体桥接实现类
///
class RedCircle implements DrawAPI {
@override
void drawCircle(int radius, int x, int y) {
print("Drawing Circle[ color: red, radius: $radius"
", x: $x, y:$y]");
}
}
class GreenCircle implements DrawAPI {
@override
void drawCircle(int radius, int x, int y) {
print("Drawing Circle[ color: green, radius: $radius"
", x: $x, y:$y]");
}
}
///
/// 使用 DrawAPI 接口创建抽象类 Shape
///
abstract class Shape {
DrawAPI _drawAPI;
Shape(this._drawAPI);
void draw();
}
///
/// 创建实现了 Shape 接口的实体类
///
class Circle extends Shape {
int _x, _y, _radius;
Circle(this._x, this._y, this._radius, DrawAPI drawAPI) : super(drawAPI);
void draw() {
_drawAPI.drawCircle(_radius, _x, _y);
}
}