现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

代码版设计模式7:桥接模式【结构型】

2013-02-24 12:09 工业·编程 ⁄ 共 1096字 ⁄ 字号 暂无评论

#include <iostream>
using namespace std;

//
struct IOS
{
public:
    virtual ~IOS() { }

    virtual void InstallOS_Imp() = 0;
};

class CWindowsOS : public IOS
{
public:
    virtual ~CWindowsOS() { }

    virtual void InstallOS_Imp() { cout<<"Windows."<<endl; }
};

class CLinuxOS : public IOS
{
public:
    virtual ~CLinuxOS() { }

    virtual void InstallOS_Imp() { cout<<"Linux."<<endl; }
};

class CUnixOS : public IOS
{
public:
    virtual ~CUnixOS() { }

    virtual void InstallOS_Imp() { cout<<"Unix."<<endl; }
};

//
struct IComputer
{
public:
    virtual ~IComputer() { }

    virtual void InstallOS(IOS *pOS) = 0;
};

class CHPComputer : public IComputer
{
public:
    virtual ~CHPComputer() { }

    virtual void InstallOS(IOS *pOS)
    {
        cout<<"HP ";
        pOS->InstallOS_Imp();
    }
};

class CAppleComputer : public IComputer
{
public:
    virtual ~CAppleComputer() { }

    virtual void InstallOS(IOS *pOS)
    {
        cout<<"Apple ";
        pOS->InstallOS_Imp();
    }
};

class CDellCompute : public IComputer
{
public:
    virtual ~CDellCompute() { }

    virtual void InstallOS(IOS *pOS)
    {
        cout<<"Dell ";
        pOS->InstallOS_Imp();
    }
};

//
void main()
{
    IOS *pOS = new CLinuxOS;

    CDellCompute Computer;
    Computer.InstallOS(pOS);

    delete pOS;
}

给我留言

留言无头像?