您的位置:首页 > 其它

漫谈设计模式--3分钟理解桥接模式:笔和画的关系

2014-10-15 11:14 239 查看
其实不需要3分钟,3秒钟就够了,记住桥接模式就是如此简单:一句话,笔有千般形,画有万变化。

下面的仅仅助于理解。

1. 定义

The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction from its implementation so that the two can vary independently"

From Wikipedia, the free encyclopedia:http://en.wikipedia.org/wiki/Bridge_pattern

2. 应用场景

The bridge pattern is useful when both the class as well as what it does vary often。

例如用笔作画,笔有铅笔、钢笔、毛笔、水笔、电脑等;图形有:直线、圆、三角形、树叶等等。各自独立变化。

3. 结构





Abstraction (abstract class)defines the abstract interfacemaintains the Implementor reference.RefinedAbstraction (normal class)extends the interface defined by AbstractionImplementor (interface)defines the interface for implementation classesConcreteImplementor (normal class)implements the Implementor interface
3. 实例

The following Java (SE 6) program illustrates the 'shape' example given below.

简单描述就是笔和图形的关系:

笔有铅笔、钢笔、毛笔、水笔等等;图形有:直线、圆、三角形、树叶等等。各自独立变化。

/** "Implementor" */
interface DrawingAPI {
public void drawCircle(double x, double y, double radius);
}

/** "ConcreteImplementor"  1/2 */
class DrawingAPI1 implements DrawingAPI {
public void drawCircle(double x, double y, double radius) {
System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);
}
}

/** "ConcreteImplementor" 2/2 */
class DrawingAPI2 implements DrawingAPI {
public void drawCircle(double x, double y, double radius) {
System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);
}
}

/** "Abstraction" */
abstract class Shape {
protected DrawingAPI drawingAPI;

protected Shape(DrawingAPI drawingAPI){
this.drawingAPI = drawingAPI;
}

public abstract void draw();                             // low-level
public abstract void resizeByPercentage(double pct);     // high-level
}

/** "Refined Abstraction" */
class CircleShape extends Shape {
private double x, y, radius;
public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) {
super(drawingAPI);
this.x = x;  this.y = y;  this.radius = radius;
}

// low-level i.e. Implementation specific
public void draw() {
drawingAPI.drawCircle(x, y, radius);
}
// high-level i.e. Abstraction specific
public void resizeByPercentage(double pct) {
radius *= pct;
}
}

/** "Client" */
class BridgePattern {
public static void main(String[] args) {
Shape[] shapes = new Shape[] {
new CircleShape(1, 2, 3, new DrawingAPI1()),
new CircleShape(5, 7, 11, new DrawingAPI2()),
};

for (Shape shape : shapes) {
shape.resizeByPercentage(2.5);
shape.draw();
}
}
}


It will output:

API1.circle at 1.000000:2.000000 radius 7.5000000
API2.circle at 5.000000:7.000000 radius 27.500000

无聊加入下面的文字,凑够博客园的文字篇幅要求,不用看。

Bridge模式的概念

Bridge 模式是构造型的设计模式之一。Bridge模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任。它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。

Bridge模式的应用场景

面向对象的程序设计(OOP)里有类继承(子类继承父类)的概念,如果一个类或接口有多个具体实现子类,如果这些子类具有以下特性:

- 存在相对并列的子类属性。

- 存在概念上的交叉。

- 可变性。

我们就可以用Bridge模式来对其进行抽象与具体,对相关类进行重构。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: