赞
踩
目录
组合/聚合的好处:优先使用对象的组合/聚合将有助于你保持每个类被封装,并被集中在单个任务上。这样类和类的继承层次会保持较小规模,并且不太可能增长为不可控制的庞然大物。
桥接模式(Bridge),将抽象部分与实现部分分离,使它们都可以独立的变化。
桥接模式的核心意图就是把这些实现对立出来,让它们各自变化。这就使得每一种实现的变化不会影响其他实习。
UML图如下(来源大话设计模式)
程序运行截图如下:
源码如下:
Head.h
- #ifndef HEAD_H
- #define HEAD_H
-
- #include <list>
- #include <string>
- #include <iostream>
- using namespace std;
-
-
- //手机软件
- class HandSetSoft{
-
- public:
- virtual void run();
- virtual ~HandSetSoft();
-
- protected:
- HandSetSoft();
- HandSetSoft(HandSetSoft &h);
- HandSetSoft &operator = (HandSetSoft &t);
- string m_name;
- };
-
- //手机游戏
- class HandSetGame : public HandSetSoft{
-
- public:
- HandSetGame(const string &name);
- ~HandSetGame();
- void run();
- };
-
- //手机通讯录
- class HandSetAddressList: public HandSetSoft{
-
- public:
- HandSetAddressList(const string &name);
- ~HandSetAddressList();
- void run();
- };
-
- //手机品牌
- class HandSetBrand{
-
- public:
- void addHandSetSoft(HandSetSoft *h);
- virtual void run();
- virtual ~HandSetBrand();
-
- protected:
- HandSetBrand();
- HandSetBrand(HandSetBrand &h);
- HandSetBrand &operator = (HandSetBrand &t);
-
- list<HandSetSoft*> m_soft;
- };
-
-
- //手机品牌N
- class HandSetBrandN: public HandSetBrand{
-
- public:
- ~HandSetBrandN();
- void run();
- };
-
-
- //手机品牌M
- class HandSetBrandM: public HandSetBrand{
-
- public:
- ~HandSetBrandM();
- void run();
- };
-
- #endif
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Head.cpp
- #include "Head.h"
-
- void HandSetSoft::run()
- {
-
- }
-
- HandSetSoft::HandSetSoft()
- {
-
- }
-
- HandSetSoft::HandSetSoft(HandSetSoft &h)
- {
-
- }
-
- HandSetSoft::~HandSetSoft()
- {
- cout << "HandSetSoft::~HandSetSoft()" << endl;
- }
-
- HandSetSoft& HandSetSoft::operator=(HandSetSoft &t)
- {
- return HandSetSoft();
- }
-
- HandSetGame::HandSetGame(const string &name)
- {
- m_name = name;
- }
-
- HandSetGame::~HandSetGame()
- {
- cout << "HandSetGame::~HandSetGame()" << endl;
- }
-
- void HandSetGame::run()
- {
- cout << "运行手机游戏 ---- " << m_name << endl;
- }
-
-
- HandSetAddressList::HandSetAddressList(const string &name)
- {
- m_name = name;
- }
-
- HandSetAddressList::~HandSetAddressList()
- {
- cout << "HandSetAddressList::~HandSetAddressList()" << endl;
- }
-
- void HandSetAddressList::run()
- {
- cout << "运行手机通讯录 ---- " << m_name << endl;
- }
-
- void HandSetBrand::addHandSetSoft(HandSetSoft *h)
- {
- m_soft.push_back(h);
- }
-
- void HandSetBrand::run()
- {
-
- }
-
- HandSetBrand::HandSetBrand()
- {
-
- }
-
- HandSetBrand::HandSetBrand(HandSetBrand &h)
- {
-
- }
-
- HandSetBrand::~HandSetBrand()
- {
- cout << "HandSetBrand::~HandSetBrand()" << endl;
- }
-
- HandSetBrand & HandSetBrand::operator=(HandSetBrand &t)
- {
- return HandSetBrand();
- }
-
- HandSetBrandN::~HandSetBrandN()
- {
- cout << "HandSetBrandN::~HandSetBrandN()" << endl;
- for(list<HandSetSoft*>::iterator i = m_soft.begin(); i != m_soft.end(); i++){
-
- delete (*i);
- }
- m_soft.clear();
- }
-
- void HandSetBrandN::run()
- {
- for(list<HandSetSoft*>::iterator i = m_soft.begin(); i != m_soft.end(); i++){
-
- (*i)->run();
- }
- }
-
- HandSetBrandM::~HandSetBrandM()
- {
- cout << "HandSetBrandM::~HandSetBrandM()" << endl;
- for(list<HandSetSoft*>::iterator i = m_soft.begin(); i != m_soft.end(); i++){
-
- delete (*i);
- }
- m_soft.clear();
- }
-
- void HandSetBrandM::run()
- {
- for(list<HandSetSoft*>::iterator i = m_soft.begin(); i != m_soft.end(); i++){
-
- (*i)->run();
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
main.cpp
- #include "Head.h"
-
- int main(int *argc, int *argv){
-
- HandSetBrand *band1 = new HandSetBrandN;
- band1->addHandSetSoft(new HandSetGame("王者荣耀"));
- band1->addHandSetSoft(new HandSetGame("吃鸡"));
- band1->addHandSetSoft(new HandSetGame("赛尔号"));
- band1->addHandSetSoft(new HandSetAddressList("流氓公司出的"));
- band1->addHandSetSoft(new HandSetAddressList("好用的软件"));
- band1->run();
- delete band1;
-
- cout << endl << "---------------华丽的分割线---------------" << endl << endl;
-
- HandSetBrand *band2 = new HandSetBrandM;
- band2->addHandSetSoft(new HandSetGame("QQ飞车"));
- band2->addHandSetSoft(new HandSetAddressList("流氓公司的"));
- band2->run();
- delete band2;
-
- getchar();
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。