结构型模式之桥接模式(Bridge)
来源:互联网 发布:罗浮威尔瓷砖 知乎 编辑:程序博客网 时间:2024/06/06 18:39
1. 意图
将抽象部分与它的实现部分分离,使它们都可以独立地变化。
2. 动机
将抽象部分与它的实现部分分离,使它们都可以独立地变化。
2. 动机
当一个抽象可能有多个实现时,通常用继承来协调它们。抽象类定义对该抽象的接口,而具体的子类则用不同方式加以实现。但是此方法有时不够灵活。继承机制将抽象部分与它的实现部分固定在一起,使得难以对抽象部分和实现部分独立地进行修改、扩充和重用。
3. 举例
就拿汽车在路上行驶的来说。即有小汽车又有公共汽车,它们都不但能在市区中的公路上行驶,也能在高速公路上行驶。这你会发现,对于交通工具(汽车)有不同的类型,然而它们所行驶的环境(路)也在变化,在软件系统中就要适应两个方面的变化?怎样实现才能应对这种变化呢?
传统做法:
桥接模式:
C++代码:
#ifndef BRIDGE_H#define BRIDGE_H#include <iostream>using std::cout;using std::endl;class CVehicle{public:virtual void Run()=0;};class CCar : public CVehicle{public:virtual void Run(){cout<<"The car is running ";}};class CBus : public CVehicle{public:virtual void Run(){cout<<"The bus is running ";}};class CRoad{private:CVehicle* m_Vehicle;public:CRoad(CVehicle* temp){m_Vehicle=temp;}virtual void Drive(){m_Vehicle->Run();}virtual void Place()=0;};class CHighSpeed : public CRoad{public:CHighSpeed(CVehicle* temp) : CRoad(temp){}virtual void Place(){cout<<"on the highspeed!"<<endl;}};class CStreet : public CRoad{public:CStreet(CVehicle* temp) : CRoad(temp){}virtual void Place(){cout<<"on the street!"<<endl;}};#endif#include <iostream>#include "bridge.h"int main(){CRoad* pRoad=new CHighSpeed(new CCar);pRoad->Drive();pRoad->Place();CRoad* pWay=new CStreet(new CBus);pWay->Drive();pWay->Place();}
1 0
- 结构型模式之桥接模式(Bridge)
- 结构型模式之桥接模式(Bridge)
- 结构型模式之桥接BRIDGE
- 设计模式(结构型)之桥接模式(Bridge Pattern)
- 【设计模式】结构型模式之桥接Bridge
- 设计模式之桥接模式(Bridge)之三(典型结构图) 结构型模式
- 结构型模式之桥模式(Bridge)
- 结构型-桥接模式(Bridge)
- Bridge桥接模式(结构型模式)
- Bridge 桥接模式(结构型模式)
- 结构型模式--Bridge桥接模式
- 结构型模式-Bridge ( 桥接模式 )
- 【结构型模式】桥接模式(Bridge)之23种java设计模式
- Java设计模式之结构型模式-桥接模式(Bridge)
- 桥接模式(Bridge)(对象结构型模式)
- Bridge 桥接模式(结构型模式) 笔记
- 结构型模式-----桥接模式(Bridge)
- 设计模式--桥接模式Bridge(结构型)
- Ubuntu在任意目录下打开terminal
- 创建 Image
- PHP7中php.ini、php-fpm和www.conf 配置
- 二叉树前序、中序、后序遍历相互求法
- struts2_11.2_OGNL(2)
- 结构型模式之桥接模式(Bridge)
- npm install -g n 运行错误
- Golang应用的多场景Dockerfile
- 使用docker compose部署服务【转】
- xajax简单例子,复制运行就明白了
- 理解 Glance
- 【厚积薄发】编程技术总结3—递归函数使用的优缺点详解
- 前端跨域问题遇到的坑及解决方式
- eclipse调试时设置浏览器