1. 桥接模式: 将抽象部分与它的实现部分分离,使它们都可以独立的变化。

分离是指 抽象类和它的派生类用来实现自己的对象分离。

实现系统可以有多角度分类,每一种分类都有可能变化,那么把这种多角度分离出来让他们独立变化,减少他们之间的耦合。

实例:

implementor.h implementor.cpp  实现接口

#ifndef IMPLEMENTOR_H
#define IMPLEMENTOR_H class Implementor
{
public:
Implementor();
void virtual operation()=0;
}; #endif // IMPLEMENTOR_H
#include "implementor.h"

Implementor::Implementor()
{
}

concreteimplementora.h concreteimplementora.cpp  具体实现功能A

#ifndef CONCRETEIMPLEMENTORA_H
#define CONCRETEIMPLEMENTORA_H #include "implementor.h" class ConcreteImplementorA : public Implementor
{
public:
ConcreteImplementorA();
void operation();
}; #endif // CONCRETEIMPLEMENTORA_H
#include "concreteimplementora.h"
#include <stdio.h> ConcreteImplementorA::ConcreteImplementorA()
{
} void ConcreteImplementorA::operation()
{
printf("ConcreteImplementorA operation\n");
}

concreteimplementorb concreteimplementorb  具体实现功能B

#ifndef CONCRETEIMPLEMENTORB_H
#define CONCRETEIMPLEMENTORB_H #include "implementor.h" class ConcreteImplementorB : public Implementor
{
public:
ConcreteImplementorB();
void operation();
}; #endif // CONCRETEIMPLEMENTORB_H
#include "concreteimplementorb.h"
#include <stdio.h> ConcreteImplementorB::ConcreteImplementorB()
{
} void ConcreteImplementorB::operation()
{
printf("ConcreteImplementorB operation\n");
}

abstraction.h abstraction.cpp 抽象

#ifndef ABSTRACTION_H
#define ABSTRACTION_H #include "implementor.h" class Abstraction
{
public:
Abstraction();
void setImplementor(Implementor *implementor);
void virtual operation(); protected:
Implementor *implementor;
}; #endif // ABSTRACTION_H
#include "abstraction.h"

Abstraction::Abstraction()
{
implementor = 0;
} void Abstraction::setImplementor(Implementor *implementor)
{
this->implementor = implementor;
} void Abstraction::operation()
{
implementor->operation();
}

refinedabstraction.h refinedabstraction.cpp 精确抽象

#ifndef REFINEDABSTRACTION_H
#define REFINEDABSTRACTION_H #include "abstraction.h" class RefinedAbstraction : public Abstraction
{
public:
RefinedAbstraction();
void operation();
}; #endif // REFINEDABSTRACTION_H
#include "refinedabstraction.h"

RefinedAbstraction::RefinedAbstraction()
{
} void RefinedAbstraction::operation()
{
implementor->operation();
}

main.cpp

#include <iostream>
#include "refinedabstraction.h"
#include "concreteimplementora.h"
#include "concreteimplementorb.h" using namespace std; int main()
{
cout << "Bridge test!" << endl; Abstraction *ab = new RefinedAbstraction();
ab->setImplementor(new ConcreteImplementorA());
ab->operation(); ab->setImplementor(new ConcreteImplementorB());
ab->operation(); return 0;
}


大话设计模式--桥接模式 Bridge -- C++实现实例的更多相关文章

  1. linkin大话设计模式--桥接模式

    linkin大话设计模式--桥接模式 桥接模式是一种结构化模式,他主要应对的是:由于实际的需要,某个类具有2个或者2个以上维度的变化,如果只是使用继承将无法实现功能,或者会使得设计变得相当的臃肿.我们 ...

  2. 转:设计模式-----桥接模式(Bridge Pattern)

    转自:http://www.cnblogs.com/houleixx/archive/2008/02/23/1078877.html 记得看原始链接的评论. 学习设计模式也有一段时间了,今天就把我整理 ...

  3. 设计模式 -- 桥接模式(Bridge)

    写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------主要内容包括: 初始桥接模式 ...

  4. 大话设计模式--工厂模式 factory -- C++实现实例

    实现<大话设计模式>的C++版本... 1. 工厂模式 使用的范围是 同一个基类,下面很多子类. (1)这里很容易出现的一个问题n多的子类继承自抽象基类,我们不得不在每次要用到子类的地方就 ...

  5. 设计模式--桥接模式Bridge(结构型)

    一.概述 在软件系统中,某些类型由于自身的逻辑,它具有两个或者多个维度的变化,如何应对这种"多维度的变化",就可以利用桥接模式. 引例: 设想如果要绘制矩形.圆形.椭圆.正方形,我 ...

  6. [工作中的设计模式]桥接模式bridge

    一.模式解析: 策略模式一节讲过,通过扩展持有者,使持有者形成抽象类,然后实现多个具体持有者,策略模式可以转化为桥接模式. 桥接模式定义为:将抽象部分与实现部分分离,使它们都可以独立的变化,在软件系统 ...

  7. C#设计模式——桥接模式(Bridge Pattern)

    一.概述在软件开发中,我们有时候会遇上一个对象具有多个变化维度.比如对汽车对象来说,可能存在不同的汽车类型,如公共汽车.轿车等,也可能存在不同的发动机,如汽油发动机.柴油发动机等.对这类对象,可应用桥 ...

  8. 大话设计模式--访问者模式 Visitor -- C++实现实例

    1. 访问者模式: 表示一个作用于某对象结构中的和元素的操作,它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 访问者模式把数据结构和作用于结构上的操作之间的耦合脱开,使得操作集合可以 ...

  9. 大话设计模式--解释器模式 interpreter -- C++实现实例

    1. 解释器模式: 给定一个语言,定义它的文法的一种表示 并 定义一个解释器,这个解释器使用该表示文法 来解释语言中的句子. 如果一种特定类型的问题发生的频率很高,那么可能就值得将该问题的各个实例表述 ...

随机推荐

  1. iOS 字符属性NSAttributedString描述【转载】

    /* 字符属性 字符属性可以应用于 attributed string 的文本中. NSString *const NSFontAttributeName;(字体) NSString *const N ...

  2. C语言中的一个*和[]优先级问题

    最近写着玩了这么一段代码 int Init(int **T, int v1, int v2, int v3) { *sizeof(int)))) exit(-); *T[]=v1, *T[]=v2, ...

  3. 蒙特卡洛方法计算圆周率的三种实现-MPI openmp pthread

    蒙特卡洛方法实现计算圆周率的方法比较简单,其思想是假设我们向一个正方形的标靶上随机投掷飞镖,靶心在正中央,标靶的长和宽都是2 英尺.同时假设有一个圆与标靶内切.圆的半径是1英尺,面积是π平方英尺.如果 ...

  4. android 软键盘监听显示和隐藏

    githup中找到:https://github.com/yescpu/KeyboardChangeListener import android.app.Activity; import andro ...

  5. 查看文档的后几行命令:tail

    假如有一个文件test.txt,内容如下: [root@lee ~]# cat test.txt 这是第1行 这是第2行 这是第3行 这是第4行 这是第5行 这是第6行 这是第7行 这是第8行 这是第 ...

  6. @media screen 针对不同移动设备——响应式设计

    概念: device-pixel-ratio:定义输入设备屏幕的可视宽度与可见高度比率. device-width:输入设备屏幕的可视宽度. orientation :屏幕横竖屏定向.landscap ...

  7. 在mac上独立安装PHP环境

    1.http://dditblog.com/blog_418.html 2.http://www.jianshu.com/p/0456dd3cc78b

  8. eclipse 给jar包关联javadoc

    1.右键点击Referenced Libraries下的jar --> 选择 Build Path --> Configure Build Path. 2.选择jar的Javadoc lo ...

  9. [luogu4315]月下“毛景树”

    [luogu4315]月下"毛景树" luogu 联赛前复习一发树剖.不会告诉你WA了4发 #define ls x<<1,l,mid #define rs x< ...

  10. [note]CRT&exCRT

    中国剩余定理 别人的blog 假设现在有关于x的同余方程组(p1,p2均为质数) \(x=a_1\pmod {p_1}\) \(x=a_2\pmod {p_2}\) 可以转化成如下形式 \(x=a_1 ...