设计模式-桥接模式
来源:互联网 发布:政府的顶级域名 编辑:程序博客网 时间:2024/06/05 09:37
桥接模式(Bridge):
将抽象部分与它的实现部分分离,使他们都可以独立地变化。
需要解释一下,什么叫做抽象与它的实现分离,这并不是说,让抽象类与其派生类分离,这没有任何意义。实现指的是抽象类和它的派生类用来实现自己的对象。也就是说手机既可以按照品牌分类又可以按照功能分类。
桥接模式代码:
#pragma once#include <string>#include <iostream>using namespace std;//implementorclass CImplementor{public:virtual void Operation() = 0;};//ConcreteImplementorA,ConcreteImplementorBclass CConcreteImplementorA : public CImplementor{public:void Operation(){cout<<"Execution method A"<<endl;}};class CConcreteImplementorB : public CImplementor{public:void Operation(){cout<<"Execution method B"<<endl;}};//Abstractionclass CAbstraction{protected:CImplementor *m_pImplementor;public:CAbstraction(){m_pImplementor = NULL;}void SetImplementor(CImplementor *pImplementor){m_pImplementor = pImplementor;}virtual void Operation() = 0;};//RefinedAbstractionclass CRefinedAbstraction : public CAbstraction{public:void Operation(){if(m_pImplementor != NULL){m_pImplementor->Operation();}}};客户端调用代码:
#include "stdafx.h"#include "BridgeMode.h"using namespace std;int main(){CAbstraction *pAb = new CRefinedAbstraction();CConcreteImplementorA *pcA = new CConcreteImplementorA();CConcreteImplementorB *pcB = new CConcreteImplementorB();pAb->SetImplementor(pcA);pAb->Operation();pAb->SetImplementor(pcB);pAb->Operation();delete pAb;delete pcA;delete pcB;return 0;}执行结果:
总结:
实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来让它们独立变化,减少它们之间的耦合。
0 0
- 设计模式----桥接模式
- 设计模式---桥接模式
- 设计模式:桥接模式
- 设计模式 桥接模式
- 设计模式 - 桥接模式
- 设计模式--桥接模式
- 设计模式--桥接模式
- 设计模式 - 桥接模式
- 设计模式-桥接模式
- 设计模式 - 桥接模式
- 设计模式 - 桥接模式
- 【设计模式-桥接模式】
- 设计模式:桥接模式
- 【设计模式】----桥接模式
- 【设计模式】桥接模式
- 设计模式--桥接模式
- 设计模式-桥接模式
- 设计模式:桥接模式
- Bitmap、CBitmap、HBITMAP以及BITMAP的相互转换
- android中的文件操作详解以及内部存储和外部存储
- 【SDOI2013】保护出题人
- UICollectionView 使用
- hadoop 开发调试环境-eclipse配置记录
- 设计模式-桥接模式
- position:static | relative | absolute | fixed
- UVA 10494 If We Were a Child Again
- dp 硬币找零问题
- UIKit框架各个类的简要说明
- 行百里者半九十,华为开发者大赛各参赛团队渐入佳境
- 协议简介之 应用层协议
- Matlab保存图片的几种方法
- Mouseenter/ mouseleave和mouseover/mouseout