漫谈设计模式--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.
简单描述就是笔和图形的关系:
笔有铅笔、钢笔、毛笔、水笔等等;图形有:直线、圆、三角形、树叶等等。各自独立变化。
It will output:
无聊加入下面的文字,凑够博客园的文字篇幅要求,不用看。
Bridge模式的概念
Bridge 模式是构造型的设计模式之一。Bridge模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任。它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。
Bridge模式的应用场景
面向对象的程序设计(OOP)里有类继承(子类继承父类)的概念,如果一个类或接口有多个具体实现子类,如果这些子类具有以下特性:
- 存在相对并列的子类属性。
- 存在概念上的交叉。
- 可变性。
我们就可以用Bridge模式来对其进行抽象与具体,对相关类进行重构。
下面的仅仅助于理解。
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模式来对其进行抽象与具体,对相关类进行重构。
相关文章推荐
- 漫谈设计模式--3分钟理解桥接模式:笔和画的关系
- Dot Net设计模式—适配器、桥接与外观三模式之间的关系
- Bridge桥接模式1 C++理解的第一个版本
- 桥接模式-通俗的理解(转)
- 理解设计模式中类的关系
- GOF23设计模式之桥接模式理解
- 桥接(Bridge)模式理解
- 深入理解JavaScript系列(44):设计模式之桥接模式
- 设计模式系列课程02之【类的关系详细理解】
- Dot Net设计模式—适配器、桥接与外观三模式之间的关系
- Bridge桥接模式2 C++理解的第二个版本
- 极速理解设计模式系列:7.桥接模式(Bridge Pattern)
- .Net中适配器、桥接与外观三模式的关系
- 深入理解JavaScript系列(44):设计模式之桥接模式
- VMware网络连接之桥接、NAt、host-only模式的理解 vm
- Dot Net设计模式—适配器、桥接与外观三模式之间的关系(转)
- 【自然框架】——页面基类与设计模式(一)桥接模式(11.14补充了一段对桥接模式的理解)
- 深入理解JavaScript系列(44):设计模式之桥接模式
- 深入理解JavaScript系列(44):设计模式之桥接模式详解
- 个人对设计模式的理解-->桥接模式