大话设计模式--桥接模式 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. 解释器模式: 给定一个语言,定义它的文法的一种表示 并 定义一个解释器,这个解释器使用该表示文法 来解释语言中的句子. 如果一种特定类型的问题发生的频率很高,那么可能就值得将该问题的各个实例表述 ...
随机推荐
- ISE封装IP
1.综合成ngc文件,然后再黑盒调用,再写一个端口文件(写个空壳文件,就是定义输入输出,在工程里面调用这个文件就行,把ngc放到工程目录下).
- 题目1 : Farthest Point
时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 Given a circle on a two-dimentional plane. Output the integral ...
- 大数据hadoop之zookeeper
一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过ZooKeeper对分布式系统进行Master选举,来解决分布式系统的单点故障,如图所示. 图 1.1 ZooK ...
- android 各版本的区别
三.Android 6.x 新增运行时权限概念 Android6.0或以上版本,用户可以完全控制应用权限.当用户安装一个app时,系统默认给app授权部分基础权限,其他敏感权限,需要开发者自己注意,当 ...
- GoogleMap-------API KEY申请流程
前言:此文是关于Google Maps Android API v2 KEY的申请流程介绍. 1.首先访问https://code.google.com/apis/console/?pli=1#pro ...
- python 补充:join() , 基本数据类型的增删改查以及深浅拷贝
# join() join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串. li = ["李李嘉诚", "麻花藤", "黄海海峰&q ...
- excel生成随机数
这个功能可以通过excel来实现,操作步骤如下: 1.新建一个excel,并打开 2.选中一个单元格,在单元格中填写: =20*RAND()+30 确定之后就会发现已经 ...
- Jmeter查看QPS和响应时间随着时间的变化曲线
以下两个插件提供测试结果,扩展图表显示 --- Response Times Over Time --- Transactions per Second 1.打开 https://jmeter-plu ...
- (比赛)A - Simple String Problem
A - Simple String Problem Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%lld & ...
- 九度OJ 1252:回文子串 (字符串处理、DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:387 解决:224 题目描述: 输入一个字符串,输出该字符串中对称的子字符串的最大长度. 比如输入字符串"google" ...