结构型模式-桥接(bridge)
来源:互联网 发布:美国创价大学 知乎 编辑:程序博客网 时间:2024/06/07 08:14
桥接
将抽象部分与它的实现部分分离,使它们都可以独立地变化。
实例
main.cc:
#include <windows.h>#include "nike_shoe.h"#include "asics_shoe.h"#include "helen_store.h"/*design_pattern:"bridge"*/int main(){ Shoe* asics_shoe = new AsicsShoe(); Shoe* nike_shoe = new NikeShoe(); Store* helen_store = new HelenStore(asics_shoe); helen_store->Selling(); Store* helen_store_2 = new HelenStore(nike_shoe); helen_store_2->Selling(); //clear delete asics_shoe; delete nike_shoe; delete helen_store; delete helen_store_2; system("Pause"); return 0;}
Store:
//store.h#ifndef HELENDP_SOURCE_STORE_H_#define HELENDP_SOURCE_STORE_H_#include "shoe.h"class Store{public: Shoe* shoe_;public: Store(Shoe *shoe); virtual ~Store(); Shoe* GetShoe(); virtual void Selling();};#endif//store.cc#include "store.h"#include <iostream>using namespace std;Store::Store(Shoe * shoe){ this->shoe_ = shoe;}Store::~Store(){}Shoe* Store::GetShoe(){ return shoe_;}void Store::Selling(){ cout << "Store Selling:not run here!" << endl;}
HelenStore:
//helen_store.h#ifndef HELENDP_SOURCE_HELEN_STORE_H_#define HELENDP_SOURCE_HELEN_STORE_H_#include "store.h"class HelenStore : public Store{public: HelenStore(Shoe *shoe); ~HelenStore(); void Selling();};#endif//helen_store.cc#include "helen_store.h"HelenStore::HelenStore(Shoe* shoe):Store(shoe){}HelenStore::~HelenStore(){}void HelenStore::Selling(){ shoe_->Sale();}
Shoe:
//shoe.h#ifndef HELENDP_SOURCE_SHOE_H_#define HELENDP_SOURCE_SHOE_H_class Shoe{public: Shoe(); virtual ~Shoe(); virtual void Sale();};#endif//shoe.cc#include "shoe.h"#include <iostream>using namespace std;Shoe::Shoe(){}Shoe::~Shoe(){}void Shoe::Sale(){ cout << "Shoe Sale: not run here!" << endl;}
AsicsShoe:
//asics_shoe.h#ifndef HELENDP_SOURCE_ASICS_SHOE_H_#define HELENDP_SOURCE_ASICS_SHOE_H_#include "shoe.h"class AsicsShoe : public Shoe{public: AsicsShoe(); ~AsicsShoe(); void Sale();};#endif//asics_shoe.cc#include "asics_shoe.h"#include <iostream>using namespace std;AsicsShoe::AsicsShoe(){}AsicsShoe::~AsicsShoe(){}void AsicsShoe::Sale(){ cout << "AsicsShoe Sale 600¥!" << endl;}
NikeShoe:
//nike_shoe.h#ifndef HELENDP_SOURCE_NIKE_SHOE_H_#define HELENDP_SOURCE_NIKE_SHOE_H_#include "shoe.h"class NikeShoe : public Shoe{public: NikeShoe(); ~NikeShoe(); void Sale();};#endif//nike_shoe.cc#include "nike_shoe.h"#include <iostream>using namespace std;NikeShoe::NikeShoe(){}NikeShoe::~NikeShoe(){}void NikeShoe::Sale(){ cout << "NikeShoe sale 500¥!" << endl;}
代码和UML图(EA)工程文件,最后会整理打包上传.
UML类图
结构
- Abstraction(Store):抽象类
- RefinedAbstraction(HelenStore):扩充抽象类
- Implementor(Shoe):实现类的接口
- ConcreteImplementor(AsicsShoe,NikeShoe):具体实现类
优点
- 分离抽象接口及其实现部分.
- 桥接模式有时类似于多继承方案,但是多继承方案违背了类的单一职责原则(即一个类只有一个变化的原因),复用性比较差,而且多继承结构中类的个数非常庞大,桥接模式是比多继承方案更好的解决方法.
- 桥接模式提高了系统的可扩充性,在两个变化维度中任意扩展一个维度,都不需要修改原有系统.
- 实现细节对客户透明,可以对用户隐藏实现细节.
缺点
- 桥接模式的引入会增加系统的理解与设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进.
0 0
- Bridge桥接(结构型模式)
- 【结构型模式】bridge(桥接)
- 结构型模式-桥接(bridge)
- Bridge 桥接(结构型模式)
- 结构型模式:Bridge(桥接)
- [设计模式-结构型]桥接(Bridge )
- 设计模式-结构型-bridge桥接
- bridge模式-结构型模式
- 设计模式之结构型模式(一)-----桥接(bridge)模式
- 设计模式笔记--结构型模式之二--桥接 Bridge
- 桥接(bridge) 模式--结构型模式之五
- 【设计模式基础】结构型模式 - 4 - 桥接(Bridge)
- 设计模式之类对象结构型 — BRIDGE (桥接)模式
- 结构型-桥接bridge
- Bridge模式——结构型模式
- Bridge桥接模式(结构型模式)
- Bridge 桥接模式(结构型模式)
- 结构型模式--Bridge桥接模式
- Eclipse插件springsource-tool-suite的下载和安装
- 跨浏览器鼠标事件写法
- 缓存与数据库一致性保证
- 初识xhprof之安装配置
- STM3日常使用笔记——启动方式
- 结构型模式-桥接(bridge)
- 指向常量的指针与指针常量
- 最近笔试题整理(二)
- yakimciwswgswis
- svn
- python + xlrd 接口自动化
- viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()
- C语言二叉树的创建
- 恢复矫正部分