首页 技术 正文
技术 2022年11月14日
0 收藏 786 点赞 3,178 浏览 2258 个字

桥接模式:

将抽象部分和它的实现部分相分离开来,以使它们能够单独地变化。

UML图:

主要包含:

  1. Abstraction:定义了抽象部分的接口。操作一个实现部分对象的引用。

  2. RefinedAbstraction:继承自抽象部分的类。
  3. Implementor:实现部分的接口。
  4. ConcreteImplementor:实现了Implementor定义的接口的详细类。

C++代码例如以下:

#include <iostream>using namespace std;class Implementor
{
public:
virtual void operationImpl()=0;
};class ConcreteImplementorA:public Implementor
{
public:
void operationImpl()
{
cout<<"ConcreteImplementorA::operationImpl"<<endl;
}};class ConcreteImplementorB:public Implementor
{
public:
void operationImpl()
{
cout<<"ConcreteImplementorB::operationImpl"<<endl;
}};class Abstraction
{
public:
virtual void operation()=0;
void setImplementor(Implementor * i)
{
impl=i;
}
Implementor * getImplementor()
{
return impl;
}
protected:
Implementor * impl;
};class RefinedAbstraction:public Abstraction
{
public:
void operation()
{
impl->operationImpl();
}
};int main()
{
cout<<"桥接模式样例"<<endl;
Abstraction * ab=new RefinedAbstraction();
Implementor * cia=new ConcreteImplementorA();
ab->setImplementor(cia);
ab->operation();
Implementor * cib=new ConcreteImplementorB();
ab->setImplementor(cib);
ab->operation();
delete cia;
delete cib;
delete ab;
return 0;
}

运行输出:


以下是一个详细的样例,看这个详细的样例可能好理解一些,摘自大话设计模式:

  1. Abstraction为Phone(手机)。
  2. RefinedAbstraction为Samsung(三星手机)。Huawei(华为手机)。
  3. Implementor为Game(手机游戏)。
  4. ConcreteImplementor为NeedForSpeed(极品飞车)。QQGame(QQ游戏),FruitNinjia(水果忍者)。

UML类图为:

C++代码:

#include <iostream>using namespace std;class Game
{
public:
virtual void play()=0;
};class NeedForSpeed :public Game
{
public:
virtual void play()
{
cout<<"need for speed play"<<endl;
}
};class QQGame :public Game
{
public:
virtual void play()
{
cout<<"QQGame play"<<endl;
}
};class FruitNinjia:public Game
{
public:
virtual void play()
{
cout<<"Fruit Ninjia play"<<endl;
}
};class Phone
{
public:
virtual void run()=0;
void setGame(Game *g)
{
game=g;
}
Game * getGame()
{
return game;
}
protected:
Game *game;
};class Samsung:public Phone
{
public:
virtual void run()
{
cout<<"Samsung :";
game->play();
}
};class HuaWei:public Phone
{
public:
virtual void run()
{
cout<<"HuaWei :";
game->play();
}};int main()
{
cout<<"桥接模式真实的样例,不同的手机品牌和手机游戏"<<endl;
Phone *samsung=new Samsung();
Phone *huawei=new HuaWei();
Game * needForSpeed=new NeedForSpeed();
Game * qqGame=new QQGame();
Game * fruit=new FruitNinjia();
samsung->setGame(qqGame);
samsung->run();
huawei->setGame(needForSpeed);
huawei->run();
samsung->setGame(fruit);
samsung->run();
delete samsung;
delete huawei;
delete needForSpeed;
delete qqGame;
delete fruit;
return 0;}

运行输出:

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918