桥接模式之C++实现

时间:2014-06-25 16:08:52   收藏:0   阅读:211

 

 

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

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

class SoftwareA : public Software
{
public:
    void Run()
    {
        cout << "运行SoftwareA" << endl;
    }
};

class SoftwareB : public Software
{
public:
    void Run()
    {
        cout << "运行SoftwareB" << endl;
    }
};

class PlatForm
{
public:
    virtual void SetSoftware(Software *) = 0;
    virtual void Run() = 0;
};

class Computer : public PlatForm
{
private:
    Software *pSoftware;
public:
    void SetSoftware(Software *soft)
    {
        pSoftware = soft;
    }

    void Run()
    {
        cout << "在电脑上";
        pSoftware->Run();
    }
};

class Phone : public PlatForm
{
private:
    Software *pSoftware;
public:
    void SetSoftware(Software *soft)
    {
        pSoftware = soft;
    }

    void Run()
    {
        cout << "在手机上";
        pSoftware->Run();
    }
};


int main()
{
    Software *pSoftwareA = new SoftwareA;
    Software *pSoftwareB = new SoftwareB;
    Phone    *pPhone     = new Phone;
    PlatForm *pComputer  = new Computer;
    
    pComputer->SetSoftware(pSoftwareA);
    pComputer->Run();
    pComputer->SetSoftware(pSoftwareB);
    pComputer->Run();

    pPhone->SetSoftware(pSoftwareA);
    pPhone->Run();
    pPhone->SetSoftware(pSoftwareB);
    pPhone->Run();

    free(pComputer);
    free(pPhone);
    free(pSoftwareB);
    free(pSoftwareA);
    return 0;
}

 

桥接模式之C++实现,布布扣,bubuko.com

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!