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. Python读取文件文件夹并检索

    import os import os.path f=open("Shouldlist.txt") ShouldList=[] while 1: line =f.readline( ...

  2. 安卓手机的后门控制工具SPADE

    SPADE,一款安卓手机的后门控制工具,安全研究人员可以以此了解和研究安卓后门原理. 首先,我们从网站www.apk4fun.com下载apk文件,如ccleaner.然后,我们安装spade git ...

  3. SpringBoot Idea 启动报错 Process finished with exit code 1

    问题描述:没有其他任何错误日志,只有Process finished with exit code 1 问题原因:Maven POM.xml问题造成 由于是properties是我直接从其他项目中拷贝 ...

  4. 机器学习2—K近邻算法学习笔记

    Python3.6.3下修改代码中def classify0(inX,dataSet,labels,k)函数的classCount.iteritems()为classCount.items(),另外p ...

  5. Apache配置压缩优化时报错——undefined symbol: inflateEnd

    Apache配置压缩优化时报错——undefined symbol: inflateEnd 环境:CentOS 6.4 软件版本:httpd-2.4.6 apr-1.4.8 apr-util-1.5. ...

  6. Harbor的搭建(vmware企业级docker镜像私服)

    1.下载harbor,地址https://github.com/vmware/harbor2.进入harbor-master/Deploy目录,修改harbor.cfg文件,主要修改以下信息      ...

  7. zabbix自动化监控三种方式

    1.agent自动注册2.sever端自动发现discovery3.zabbix api

  8. args *args **kwargs区别

    python 函数中的参数类型有两种,分别为 位置参数和关键字参数: 一 .位置参数(该类参数位置固定不变) args:     表示默认位置参数,该参数是具象的,有多少个参数就传递多少参数,且参数位 ...

  9. UFLDL深度学习笔记 (五)自编码线性解码器

    UFLDL深度学习笔记 (五)自编码线性解码器 1. 基本问题 在第一篇 UFLDL深度学习笔记 (一)基本知识与稀疏自编码中讨论了激活函数为\(sigmoid\)函数的系数自编码网络,本文要讨论&q ...

  10. Android双缓冲技术

    参考文章: 1.http://djt.qq.com/article/view/987 2.http://blog.csdn.net/i_lovefish/article/details/7913623 ...