大话设计模式--桥接模式 Bridge -- C++实现实例
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++实现实例的更多相关文章
- linkin大话设计模式--桥接模式
linkin大话设计模式--桥接模式 桥接模式是一种结构化模式,他主要应对的是:由于实际的需要,某个类具有2个或者2个以上维度的变化,如果只是使用继承将无法实现功能,或者会使得设计变得相当的臃肿.我们 ...
- 转:设计模式-----桥接模式(Bridge Pattern)
转自:http://www.cnblogs.com/houleixx/archive/2008/02/23/1078877.html 记得看原始链接的评论. 学习设计模式也有一段时间了,今天就把我整理 ...
- 设计模式 -- 桥接模式(Bridge)
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------主要内容包括: 初始桥接模式 ...
- 大话设计模式--工厂模式 factory -- C++实现实例
实现<大话设计模式>的C++版本... 1. 工厂模式 使用的范围是 同一个基类,下面很多子类. (1)这里很容易出现的一个问题n多的子类继承自抽象基类,我们不得不在每次要用到子类的地方就 ...
- 设计模式--桥接模式Bridge(结构型)
一.概述 在软件系统中,某些类型由于自身的逻辑,它具有两个或者多个维度的变化,如何应对这种"多维度的变化",就可以利用桥接模式. 引例: 设想如果要绘制矩形.圆形.椭圆.正方形,我 ...
- [工作中的设计模式]桥接模式bridge
一.模式解析: 策略模式一节讲过,通过扩展持有者,使持有者形成抽象类,然后实现多个具体持有者,策略模式可以转化为桥接模式. 桥接模式定义为:将抽象部分与实现部分分离,使它们都可以独立的变化,在软件系统 ...
- C#设计模式——桥接模式(Bridge Pattern)
一.概述在软件开发中,我们有时候会遇上一个对象具有多个变化维度.比如对汽车对象来说,可能存在不同的汽车类型,如公共汽车.轿车等,也可能存在不同的发动机,如汽油发动机.柴油发动机等.对这类对象,可应用桥 ...
- 大话设计模式--访问者模式 Visitor -- C++实现实例
1. 访问者模式: 表示一个作用于某对象结构中的和元素的操作,它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 访问者模式把数据结构和作用于结构上的操作之间的耦合脱开,使得操作集合可以 ...
- 大话设计模式--解释器模式 interpreter -- C++实现实例
1. 解释器模式: 给定一个语言,定义它的文法的一种表示 并 定义一个解释器,这个解释器使用该表示文法 来解释语言中的句子. 如果一种特定类型的问题发生的频率很高,那么可能就值得将该问题的各个实例表述 ...
随机推荐
- MapReduce小文件处理之CombineFileInputFormat实现
在MapReduce使用过程中.一般会遇到输入文件特别小(几百KB.几十MB).而Hadoop默认会为每一个文件向yarn申请一个container启动map,container的启动关闭是很耗时的. ...
- Linux个人常用命令汇总
1. 查询端口状态 netstat -apn | grep [port] 得到pid=64472 2. 根据进程id查询程序信息 ps -aux | grep [pid] 3. CentOS防火墙 参 ...
- Python环境搭建及IDE选择(转载)
Python环境搭建及IDE选择 人工智能社区 http://studyai.com 系统:Windows 7 版本:Python 2.7 一.安装Python 在开始编程之前,我们首先需要搭建Pyt ...
- IDEA中配置svn
反正这个命令行我是勾选了的,软件占用内存不大,我就都选了. 然后是配置IDEA 试一下效果,果然阔以,哈哈.
- Ubuntu配置apache2.4配置虚拟主机遇到的问题
update: 偶然看到了 apache的更新说明,直接贴个地址过来吧. http://httpd.apache.org/docs/2.4/upgrading.html 最近想把web开发目录从/va ...
- LNMP环境搭建之php安装,wordpress博客搭建
LNMP环境搭建之php安装,wordpress博客搭建 一.介绍: 1.什么是CGI CGI全称是"通用网关接口"(Common Gateway Interface),HTTP服 ...
- subject相关信息
记录一下我的错误信息 在ssm+shiro整合的时候,发现这样一个问题,第一次登录进去之后,然后再登录,输入错误的用户名和密码,竟然仍然登录成功,突然想起来了,是不是login之后,logout之后这 ...
- python获取shell输出(转)
From:http://www.cnblogs.com/snow-backup/p/5035792.html python中获取shell命令输出的方法: 1. import subproces ...
- Centos date 设置自定义时间
[1]手动修改 (1)设置日期 # date -s 20190315 (2)设置时间 # date -s 15:23:34 (3)设置日期和时间 # date -s "20190315 15 ...
- poj1061(extendgcd)
看完题目后,题目要求: 设时间为t (x+mt)%L = (y+nt)%L ( x-y + (m-n)*t )= k*L (k是整数,可为负) 然后就是经典的 xa+yb=c 求解x,y的经典题目了. ...