桥接模式之C++实现

 

 

#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;
}

 

你可能感兴趣的:(桥接模式)