桥接模式(Bridge )-设计模式(七)
来源:互联网 发布:广联达计价对比软件 编辑:程序博客网 时间:2024/05/22 14:14
作用:
将抽象部分与它的实现部分分离, 使它们都可以独立地变化。
解析:
Bridge 用于将表示和实现解耦,两者可以独立的变化.在 Abstraction 类中维护一个 Implementor 类指针, 需要采用不同的实现方式的时候只需要传入不同的Implementor 派生类就可以了.
Bridge 的实现方式其实和 Builde 十分的相近,可以这么说:本质上是一样的, 只是封装的东西不一样罢了.两者的实现都有如下的共同点:抽象出来一个基类,这个基类里面定义了共有的一些行为,形成接口函数(对接口编程而不是对实现编程),这个接口函数在 Buildier 中是 BuildePart 函数在 Bridge 中是 OperationImpl 函数;其次,聚合一个基类的指针,如 Builder 模式中 Director 类聚合了一个 Builder 基类的指针,而 Brige 模式中 Abstraction 类聚合了一个 Implementor 基类的指针(优先采用聚合而不是继承);而在使用的时候, 都把对这个类的使用封装在一个函数中, 在 Bridge 中是封装在Director::Construct 函数中, 因为装配不同部分的过程是一致的,而在 Bridge 模式中则是封装在 Abstraction::Operation 函数中,在这个函数中调用对应的 Implementor::OperationImp 函数.就两个模式而言,Builder 封装了不同的生成组成部分的方式, 而 Bridge 封装了不同的实现方式.因此,如果以一些最基本的面向对象的设计原则来分析这些模式的实现的话,还是可以看到很多共同的地方的.
举个例子吧!
小汽车可以在高速公路和城市街区跑,大巴也可以,此时对于交通工具(汽车)有不同的类型,而对于路也有不同的类型,而我们要适应两个方面的变化。
看一下实现:(实现跟例子无关)
#include<iostream>using namespace std;//为实现Abstraction定义的抽象基类,定义了实现的接口函数class Implementor{ public: Implementor(){} virtual ~Implementor(){} virtual void OperationImp()=0;};//继承自Implementor,是Implementor的不同实现之一class ConcreteImplementorA:public Implementor{ public: ConcreteImplementorA(){} virtual ~ConcreteImplementorA(){} virtual void OperationImp() { cout<<"Implementation by ConcreteImplementorA"<<endl; }};//继承自Implementor,是Implementor的不同实现之一class ConcreteImplementorB:public Implementor{ public: ConcreteImplementorB(){} virtual ~ConcreteImplementorB(){} virtual void OperationImp() { cout<<"Implementation by ConcreteImplementorB"<<endl; }};//维护一个Implementor类的指针class Abstraction{ public: Abstraction(Implementor* pImplementor):m_pImplementor(pImplementor){} virtual ~Abstraction() { delete m_pImplementor; m_pImplementor = NULL; } void Operation() { m_pImplementor->OperationImp(); } protected: Implementor* m_pImplementor;};int main(){ Implementor* pImpA = new ConcreteImplementorA(); Abstraction* pAbstraction1 = new Abstraction(pImpA); pAbstraction1->Operation(); Implementor* pImpB = new ConcreteImplementorB(); Abstraction* pAbstraction2 = new Abstraction(pImpB); pAbstraction2->Operation(); delete pAbstraction1; delete pAbstraction2; return 0;}
- 设计模式之(七)--桥接模式(bridge)
- 桥接模式(Bridge )-设计模式(七)
- 设计模式(七)之Bridge - 桥接模式
- 设计模式(七):桥接模式Bridge(结构型模式)
- 设计模式:桥接模式(Bridge)
- 设计模式:桥接模式(Bridge)
- 设计模式:桥接模式(Bridge)
- 设计模式:桥接模式(Bridge)
- 【设计模式】桥接模式(Bridge)
- 设计模式(七)----- 桥接模式(Bridge)----(JAVA版)
- 设计模式之 Bridge 模式(桥接模式)
- 每天一个(设计模式)-- Bridge模式(桥接模式)
- 设计模式之Bridge(桥模式)
- 七、桥接模式Bridge(结构型)
- 《设计模式精解》学习笔记(七)------Bridge(桥)模式
- 设计模式实现(十八)---桥接模式(Bridge)
- 设计模式之(十) Bridge(桥接模式)
- (10)Java设计模式-桥接模式(Bridge)
- Intent 和 Intent Filter
- 应用程序单独启动
- Delphi线程同步(临界区、互斥、信号量)
- Android中this、super的区别
- no applicable items eclipse
- 桥接模式(Bridge )-设计模式(七)
- 如何在线查看某个地方的高程值
- 其他信息: 线程间操作无效: 从不是创建控件“stateLabel”的线程访问它。解决办法
- app_name is not translate
- 动态的Json数据和静态的Json数据
- mac系统下按住mysql-python
- +id/image, already defined earlier in this layout
- android.support.v7.internal.widget.ActionBarView.HomeView
- android常用变量设置