C++实现设计模式:Bridge Pattern


注:为了简要说明,类的成员函数均采用inline。

#ifndef _DRIVE_GAME_H_
#define _DRIVE_GAME_H_

#include "DriveGameImpl.h"

#include <iostream>
using std::cout;
using std::endl;

class DriveGame
{
public:
    DriveGame(DriveGameImpl* impl):_impl(impl){}

    virtual ~DriveGame(){}
    virtual void init()
    {
        cout<<"Init the game."<<endl;
        _impl->display();
        _impl->flush();
    }
    virtual void startGame() = 0
    {
        //Omit for brief description.
    }
    
private:
    DriveGameImpl* _impl;
};

class DriveGame_3D:public DriveGame
{
public:
    DriveGame_3D(DriveGameImpl* impl):DriveGame(impl)
    {
        //Omit for brief description.
    }

    ~DriveGame_3D()
    {
        //To clear the status of the game.
    }

    virtual void startGame()
    {
        init();
        cout<<"Start 3D Drive Game."<<endl;
    }

private:
    //Omit for brief description.
};

class DriveGame_2D:public DriveGame
{
public:
    DriveGame_2D(DriveGameImpl* impl):DriveGame(impl)
    {
        //
    }

    ~DriveGame_2D()
    {
        //To clear the status of the game.
    }
    virtual void startGame()
    {
        init();
        cout<<"Start 2D Drive Game."<<endl;
    }

private:
    //Omit for brief description.
};

#endif



#ifndef _DRIVE_GAME_IMPL_H_
#define _DRIVE_GAME_IMPL_H_

#include <iostream>
using std::cout;
using std::endl;
using std::string;

class DriveGameImpl
{
public:
    virtual ~DriveGameImpl(){}
    virtual void display()=0{}
    virtual void flush()=0{}
    
};

class LinuxDriveGameImpl:public DriveGameImpl
{
public:
    ~LinuxDriveGameImpl(){}
    void display()
    {
        cout<<"Linux system: display the game."<<endl;
    }
    void flush()
    {
        cout<<"Linux system: flush the game."<<endl;
    }
};

class WindowsDriveGameImpl:public DriveGameImpl
{
public:
    ~WindowsDriveGameImpl(){}
    void display()
    {
        cout<<"windows system: display the game."<<endl;
    }
    void flush()
    {
        cout<<"windows system: flush the game."<<endl;
    }
};

#endif


#include <iostream>

#include "DriveGame.h"
#include "DriveGameImpl.h"

using std::cout;
using std::endl;
using std::string;

int main()
{
    DriveGameImpl* linuxGameImpl = new LinuxDriveGameImpl();
    DriveGame* game_2D_Linux = new DriveGame_2D(linuxGameImpl);
    game_2D_Linux->startGame();
    
    system("PAUSE");
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值