设计模式 桥接模式

1、game.h

#ifndef GAME_H
#define GAME_H

#include <iostream>
#include <string>
using namespace std;

#include "gameimpl.h"


class Game //抽象部分的基类. Abstraction
{
public:
    Game(GameImpl* pGameImpl)
        :mpGameImpl(pGameImpl)
    {
    };
    virtual ~Game(){};
    virtual void Play(){};
protected:
    GameImpl* mpGameImpl;
};
#endif // GAME_H

2、gameimpl.h

#ifndef GAMEIMPL_H
#define GAMEIMPL_H

#include <iostream>
#include <string>
using namespace std;


class GameImpl //实现部分的基类 Implementor
{
public:
    GameImpl(){};
    virtual ~GameImpl(){};
    virtual void PlayImpl() = 0;
};

#endif // GAMEIMPL_H

3-1、badmintongame.h

#ifndef BADMINTONGAME_H
#define BADMINTONGAME_H

#include <iostream>
#include <string>
using namespace std;

#include "game.h"
#include "gameimpl.h"


class BadmintonGame : public Game //羽毛球的抽象实现
{
public:
    BadmintonGame(GameImpl* pGameImpl);
    ~BadmintonGame(){};
    void Play();
};
#endif // BADMINTONGAME_H

3-2、badmintongame.cpp

#include "badmintongame.h"


BadmintonGame::BadmintonGame(GameImpl *pGameImpl)
    :Game(pGameImpl)
{
}

void BadmintonGame::Play()
{
    cout << "I am playing badminton game" << endl;
    mpGameImpl->PlayImpl();
}

4-1、tennisgame.h

#ifndef TENNISGAME_H
#define TENNISGAME_H

#include <iostream>
#include <string>
using namespace std;

#include "game.h"
#include "gameimpl.h"

class TennisGame : public Game //网球的抽象实现
{
public:
    TennisGame(GameImpl* pGameImpl);
    ~TennisGame(){};
    void Play();
};
#endif // TENNISGAME_H

4-2、tennisgame.cpp

#include "tennisgame.h"


TennisGame::TennisGame(GameImpl *pGameImpl)
    :Game(pGameImpl)
{
}

void TennisGame::Play()
{
    cout << "I am playing tennis game" << endl;
    mpGameImpl->PlayImpl();
}

5-1、linuxgameimpl.h

#ifndef LINUXGAMEIMPL_H
#define LINUXGAMEIMPL_H

#include <iostream>
#include <string>
using namespace std;

#include "gameimpl.h"


class LinuxGameImpl : public GameImpl //Linux 系统上的具体实现
{
public:
    LinuxGameImpl(){};
    ~LinuxGameImpl(){};
    void PlayImpl();
};
#endif // LINUXGAMEIMPL_H

5-2、linuxgameimpl.cpp

#include "linuxgameimpl.h"


void LinuxGameImpl:: PlayImpl()
{
    cout << "this game is running on Linux" << endl;
}

6-1、winxpgameimpl.h

#ifndef WINXPGAMEIMPL_H
#define WINXPGAMEIMPL_H

#include <iostream>
#include <string>
using namespace std;

#include "gameimpl.h"


class WinxpGameImpl : public GameImpl //WinXP 系统上的具体实现
{
public:
    WinxpGameImpl(){};
    ~WinxpGameImpl(){};
    void PlayImpl();
};
#endif // WINXPGAMEIMPL_H

6-2、winxpgameimpl.cpp

#include "winxpgameimpl.h"


void WinxpGameImpl::PlayImpl()
{
    cout << "this game is running on WinXP" << endl;
}

7、main.cpp
/*
作者:jhluroom弹   QQ:454676244  MSN:jhlu0815@hotmail.com
开发IDE:qt creater
开发环境:QT C++
参考网站:神秘果:http://www.shenmiguo.com/

定义:
桥接模式:将抽象部分与它的实现部分分离,使它们都可以独立地变化。也叫Handle。

理解:
1.Abstraction是对外提供接口的抽象类,由客户端调用。它聚合了一个指向接口实现基类Implementor的指针。
2.Implementor是实现类的基类。它与Abstraction的接口不一定相同。
  一般来说,Implementor提供基本操作,Abstraction定义了基于基本操作的更高层次操作。

要点:
1.桥接模式用来解决一个模块两个维度(抽象和实现)的变化。一个维度提供抽象接口功能(Abstraction);
  另一个维度是抽象接口功能的实现方式也在变化(Implementor)。
2.桥接模式与适配器(Adapter)模式的区别:
  适配器模式使两个完全不同的接口实现兼容,是设计后的行为;
  桥接模式是设计之初就考虑到了,目的是抽象接口和实现部分可以独立的变化。

应用:
羽毛球游戏和网球游戏,都是一种游戏,可以运行于WinXP系统之上,同时也可运行于Linux系统之上。
LinuxGameImpl 、WinxpGameImpl,提供基本操;
TennisGame 、BadmintonGame 定义了基于基本操作的更高层次操作;

以上文字说明,从网上整理而来,有可能部分与其他同仁相同,请谅解,希望我们能够共同交流,谢谢!
*/

#include <QtCore/QCoreApplication>

#include "game.h"
#include "gameimpl.h"
#include "linuxgameimpl.h"
#include "winxpgameimpl.h"
#include "tennisgame.h"
#include "badmintongame.h"


int main(int argc, char *argv[])
{
    cout << "=== jhluroom start ========" << endl;

    //在WinXP系统上实现网球游戏
    GameImpl* pGameImplWinxp = new WinxpGameImpl;
    Game* pGameTennisOnWinxp = new TennisGame(pGameImplWinxp);
    pGameTennisOnWinxp->Play();

    //在Linux系统上实现习毛球游戏
    GameImpl* pGameImplLinux = new LinuxGameImpl;
    Game* pGameBadminitonOnLinux = new BadmintonGame(pGameImplLinux);
    pGameBadminitonOnLinux->Play();

    delete pGameImplWinxp;
    delete pGameTennisOnWinxp;
    delete pGameImplLinux;
    delete pGameBadminitonOnLinux;

    cout << "=== jhluroom finish _^_ ===" << endl;
    return 0;
}

运行结果:

=== jhluroom start ========

I am playing tennis game
this game is running on WinXP
I am playing badminton game
this game is running on Linux

=== jhluroom finish _^_ ===


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值