C++ 桥接模式(Bridge)

简介

桥接模式(Bridge):将抽象部分与它的实现部分分离,使他们都能够独立地变化。

作用

解决在有多种可能会变化的情况下,用继承会造成类越来越复杂问题,扩展起来不灵活。

应用场景

设计中有超过一维的变化我们就可以用桥模式。如果只有一维在变化,那么我们用继承就可以圆满的解决问题。

代码如下:

#include <iostream>
#include <string>
#include <memory>

///
//软件基类
class software
{
public:
    software(void){}
    virtual ~software(void){}

public:
    virtual void opt(void) = 0;
};
//手机基类
class mobilephone
{
public:
    mobilephone(void){}
    virtual ~mobilephone(void){}

public:
    virtual void opt() = 0;
};
class mobilephone_sw
{
public:
    mobilephone_sw(std::shared_ptr<software> sw)
		: m_software(sw)
	{
			
	}
    virtual ~mobilephone_sw(void){}

public:
    virtual void opt() = 0;
	std::shared_ptr<software> m_software;
};
///
//苹果手机
class iphone : public mobilephone_sw
{
public:
    iphone(std::shared_ptr<software> sw) : mobilephone_sw(sw){}
    virtual ~iphone(void){}

public:
    void opt(void)
	{
		std::cout << "opt : this is iphone" << std::endl;
		m_software->opt();
	}
};
//华为手机
class huawei : public mobilephone_sw
{
public:
    huawei(std::shared_ptr<software> sw): mobilephone_sw(sw){}
    virtual ~huawei(void){}

public:
    void opt(void)
	{
		std::cout << "opt : this is huawei" << std::endl;
		m_software->opt();
	}

};
///
//软件 :王者荣耀
class wangzhe : public software
{
public:
    wangzhe(void){}
    virtual ~wangzhe(void){}

public:
    virtual void opt(void)
	{
		std::cout << "opt : 王者荣耀 !" << std::endl;
	}
};
//软件 : 淘宝
class taobao : public software
{
public:
    taobao(void){}
    virtual ~taobao(void){}

public:
    virtual void opt(void)
	{
		std::cout << "opt : 淘宝 !" << std::endl;
	}
};
///
int main()
{
	std::cout << "start-up .." << std::endl;
	std::shared_ptr<wangzhe> p_wz = std::make_shared<wangzhe>();
	std::shared_ptr<taobao>  p_tb = std::make_shared<taobao>();
	
	std::shared_ptr<iphone> p_ip = std::make_shared<iphone>(p_wz);
	std::shared_ptr<huawei> p_hw = std::make_shared<huawei>(p_tb);
	
    p_ip->opt();
    p_hw->opt();
	
	std::cout << "done .." << std::endl;
    return 0;
}

运行结果:
在这里插入图片描述

  • 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、付费专栏及课程。

余额充值