码迷,mamicode.com
首页 > 编程语言 > 详细

桥接模式之C++实现

时间:2014-06-25 16:08:52      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   color   com   

 

 

#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

桥接模式之C++实现

标签:style   class   blog   code   color   com   

原文地址:http://www.cnblogs.com/jingmoxukong/p/3806340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!