桥接模式(bridge)c++版

这是大话设计模式中桥接模式的c++版本

/*
 * bridge.cpp
 *
 *  Created on: Oct 13, 2017
 *      Author: clh01s@163.com
 *      桥接模式:将抽象部分与它的实现部分分离,
 *              使它们都可以独立的变化。
 *              实现指的是抽象类和他的派生类用来实现自己的对象。
 *
 */

#include <iostream>
using namespace std;

class HandsetSoft
{
public:
    virtual ~HandsetSoft(){}
    virtual void Run()=0;
};

//手机游戏
class HandsetGame:public HandsetSoft
{
public:
    //实现run函数
    void Run() override
    {
        cout<<"运行手机游戏。"<<endl;
    }
};

class HandsetAddressList:public HandsetSoft
{
public:
    //实现run函数
    void Run() override
    {
        cout<<"运行手机通讯录。"<<endl;
    }
};

//手机品牌
class HandsetBrand
{
public:
    virtual ~HandsetBrand(){}
    //设置手机软件
    void SetHandsetSoft(HandsetSoft* soft){soft_ = soft;}
    virtual void Run()=0;
protected:
    HandsetSoft* soft_;
};

//手机品牌N
class HandsetBrandN:public HandsetBrand
{
public:
    //运行软件
    void Run() override
    {
        cout<<"N手机运行软件"<<endl;
        HandsetBrand::soft_->Run();
    }
};

//手机品牌M
class HandsetBrandM:public HandsetBrand
{
public:
    //运行软件
    void Run() override
    {
        cout<<"M手机运行软件"<<endl;
        HandsetBrand::soft_->Run();
    }
};

int main()
{
//实例化手机品牌N
    HandsetBrandN abn;
    abn.SetHandsetSoft(new HandsetGame());
    abn.Run();

    abn.SetHandsetSoft(new HandsetAddressList());
    abn.Run();
//实例化手机品牌M
    HandsetBrandM abm;
    abm.SetHandsetSoft(new HandsetGame());
    abm.Run();

    abm.SetHandsetSoft(new HandsetAddressList());
    abm.Run();

    return 0;
}

运行结果:

clh@clh:~/testcode/设计模式$ ./a.out 
N手机运行软件
运行手机游戏。
N手机运行软件
运行手机通讯录。
M手机运行软件
运行手机游戏。
M手机运行软件
运行手机通讯录。

以下内容摘抄自《设计模式》
桥接模式的动机:
当一个抽象可能有多个实现时,通常用继承来协调它们。抽象类定义对该抽象的接口,而具体的子类则用不同的方式加以实现。但是此方法有时不够灵活。继承机制将抽象部分与它的实现部分固定在一起,使得难以对抽象部分和实现部分独立的进行修改、扩充和重用。

桥接模式的使用情况:
1.你希望在抽象和它的实现部分之间有一个固定的绑定关系。例如这种情况可能是因为,在程序运行时刻实现部分应该可以被选择或者切换。
2.类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。这时bridge模式使你可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充。
3.对一个抽象的实现部分的修改应对客户不产生影响,即客户代码不必重新编译。
4.(c++)你相对客户完全隐藏抽象的实现部分。在C++中,类的表示在类的接口中是可见的。
5.如果你需要生成许多相同功能的类(比如不同系统中的相同的功能的类)。这样一种情况说明你必须将一个对象分解成连个部分。
6.你想在多个对象间共享实现(可能使用引用计数)。但同时要求客户并不知道这一点。

桥接模式的优点:
1.分离接口以及实现部分 一个实现未必不变的绑定在一个接口上。抽象类的实现可以在运行时刻进行配置,一个对象甚至可以在运行时刻改变他的实现。而且改变一个实现类的时候客户不必要重新进行编译。
另外接口与实现的分离有助于分层,从而产生更好的结构化系统,系统高层仅需要知道abstraction和implementor即可。
2.提高可扩充性 可以独立的扩充abstraction和implementor。
3.实现细节对用户透明 你可以对用户隐藏细节,例如共享implementor对象以及相应的引用计数机制(如果有的话)
转载请注明原地址:http://blog.csdn.net/clh01s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了更好地理解桥接模式,我们可以考虑一个简单的例子:一个音乐播放器程序。该程序需要支持不同的音频格式(例如MP3,WAV,FLAC等),并且还需要能够在不同的操作系统上运行(例如Windows,MacOS,Linux等)。这个问题可以使用桥接模式来解决。 首先,我们需要定义音频播放器接口,包括播放,暂停和停止功能: ``` // 音频播放器接口 typedef struct _MediaPlayer MediaPlayer; struct _MediaPlayer { void (*play)(MediaPlayer*); void (*pause)(MediaPlayer*); void (*stop)(MediaPlayer*); }; ``` 然后,我们定义不同音频格式的播放器实现: ``` // MP3 播放器实现 typedef struct _MP3PlayerImpl MP3PlayerImpl; struct _MP3PlayerImpl { MediaPlayer super; // 为 MP3 文件格式添加特殊的功能 }; // WAV 播放器实现 typedef struct _WAVPlayerImpl WAVPlayerImpl; struct _WAVPlayerImpl { MediaPlayer super; // 为 WAV 文件格式添加特殊的功能 }; // FLAC 播放器实现 typedef struct _FLACPlayerImpl FLACPlayerImpl; struct _FLACPlayerImpl { MediaPlayer super; // 为 FLAC 文件格式添加特殊的功能 }; ``` 最后,我们定义操作系统的平台实现: ``` // Windows 操作系统平台实现 typedef struct _WindowsPlatformImpl WindowsPlatformImpl; struct _WindowsPlatformImpl { MediaPlayer* (*createMediaPlayer)(WindowsPlatformImpl*); // 为 Windows 操作系统添加特殊的功能 }; // MacOS 操作系统平台实现 typedef struct _MacOSPlatformImpl MacOSPlatformImpl; struct _MacOSPlatformImpl { MediaPlayer* (*createMediaPlayer)(MacOSPlatformImpl*); // 为 MacOS 操作系统添加特殊的功能 }; // Linux 操作系统平台实现 typedef struct _LinuxPlatformImpl LinuxPlatformImpl; struct _LinuxPlatformImpl { MediaPlayer* (*createMediaPlayer)(LinuxPlatformImpl*); // 为 Linux 操作系统添加特殊的功能 }; ``` 最后,我们可以使用桥接模式来组合不同的实现: ``` // 桥接模式 typedef struct _MediaPlayerBridge MediaPlayerBridge; struct _MediaPlayerBridge { MediaPlayer* (*createMediaPlayer)(MediaPlayerBridge*); void (*setPlatformImpl)(MediaPlayerBridge*, void*); void (*setAudioImpl)(MediaPlayerBridge*, void*); void* platformImpl; void* audioImpl; }; MediaPlayer* MediaPlayerBridge_createMediaPlayer(MediaPlayerBridge* bridge) { MediaPlayer* player = bridge->createMediaPlayer(bridge); return player; } void MediaPlayerBridge_setPlatformImpl(MediaPlayerBridge* bridge, void* impl) { bridge->platformImpl = impl; } void MediaPlayerBridge_setAudioImpl(MediaPlayerBridge* bridge, void* impl) { bridge->audioImpl = impl; } // Windows 平台的播放器 MediaPlayer* WindowsPlatformImpl_createMediaPlayer(WindowsPlatformImpl* impl) { MP3PlayerImpl* player = (MP3PlayerImpl*)malloc(sizeof(MP3PlayerImpl)); player->super.play = MP3Player_play; player->super.pause = MP3Player_pause; player->super.stop = MP3Player_stop; // 添加 Windows 平台特殊的实现 return (MediaPlayer*)player; } // MacOS 平台的播放器 MediaPlayer* MacOSPlatformImpl_createMediaPlayer(MacOSPlatformImpl* impl) { WAVPlayerImpl* player = (WAVPlayerImpl*)malloc(sizeof(WAVPlayerImpl)); player->super.play = WAVPlayer_play; player->super.pause = WAVPlayer_pause; player->super.stop = WAVPlayer_stop; // 添加 MacOS 平台特殊的实现 return (MediaPlayer*)player; } // Linux 平台的播放器 MediaPlayer* LinuxPlatformImpl_createMediaPlayer(LinuxPlatformImpl* impl) { FLACPlayerImpl* player = (FLACPlayerImpl*)malloc(sizeof(FLACPlayerImpl)); player->super.play = FLACPlayer_play; player->super.pause = FLACPlayer_pause; player->super.stop = FLACPlayer_stop; // 添加 Linux 平台特殊的实现 return (MediaPlayer*)player; } int main() { MediaPlayerBridge bridge; bridge.createMediaPlayer = NULL; bridge.setPlatformImpl = MediaPlayerBridge_setPlatformImpl; bridge.setAudioImpl = MediaPlayerBridge_setAudioImpl; bridge.platformImpl = NULL; bridge.audioImpl = NULL; // Windows 平台的 MP3 播放器 WindowsPlatformImpl windowsImpl; windowsImpl.createMediaPlayer = WindowsPlatformImpl_createMediaPlayer; bridge.setPlatformImpl(&bridge, &windowsImpl); MP3PlayerImpl mp3Impl; bridge.setAudioImpl(&bridge, &mp3Impl); MediaPlayer* player = MediaPlayerBridge_createMediaPlayer(&bridge); player->play(player); // MacOS 平台的 WAV 播放器 MacOSPlatformImpl macosImpl; macosImpl.createMediaPlayer = MacOSPlatformImpl_createMediaPlayer; bridge.setPlatformImpl(&bridge, &macosImpl); WAVPlayerImpl wavImpl; bridge.setAudioImpl(&bridge, &wavImpl); player = MediaPlayerBridge_createMediaPlayer(&bridge); player->play(player); // Linux 平台的 FLAC 播放器 LinuxPlatformImpl linuxImpl; linuxImpl.createMediaPlayer = LinuxPlatformImpl_createMediaPlayer; bridge.setPlatformImpl(&bridge, &linuxImpl); FLACPlayerImpl flacImpl; bridge.setAudioImpl(&bridge, &flacImpl); player = MediaPlayerBridge_createMediaPlayer(&bridge); player->play(player); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值