c++桥接模式

可以简记为pointer to implement:”指向实现的指针”。

 [DP]书上定义:将抽象部分与它的实现部分分离,使它们都可以独立地变化。考虑装操作系统,有多种配置的计算机,同样也有多款操作系统。如何运用桥接模式呢?可以将操作系统和计算机分别抽象出来,让它们各自发展,减少它们的耦合度。当然了,两者之间有标准的接口。这样设计,不论是对于计算机,还是操作系统都是非常有利的。下面给出这种设计的UML图,其实就是桥接模式的UML图。

 

#include <bits/stdc++.h>
using namespace std;

#define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl

class COS
{
public:
    COS() {cout << "OS()" << endl;}
    virtual ~COS() {cout << "~OS()" << endl;}
    virtual void Run(){}
};

class CLinuxOS : public COS
{
public:
    CLinuxOS() {cout << "CLinuxOS()" << endl;}
    ~CLinuxOS() {cout << "~CLinuxOS()" << endl;}
    void Run(){cout << "LinuxOS run()" << endl;}
};

class CUnixOS : public COS
{
public:
    CUnixOS() {cout << "CUnixOS()" << endl;}
    ~CUnixOS() {cout << "~CUnixOS()" << endl;}
    void Run(){cout << "CUnixOS run()" << endl;}
};

class CComputer
{
public:
    CComputer() {cout << "CComputer()" << endl;}
    virtual ~CComputer() {cout << "~CComputer()" << endl;}
    virtual void InstallOS(COS *os){}
};

class CIBMComputer : public CComputer
{
public:
    CIBMComputer() {cout << "CIBMComputer()" << endl;}
    ~CIBMComputer() {cout << "~CIBMComputer()" << endl;}
    void InstallOS(COS *os){ os->Run();}
};

class CHPComputer : public CComputer
{
public:
    CHPComputer() {cout << "CHPComputer()" << endl;}
    ~CHPComputer() {cout << "~CHPComputer()" << endl;}
    void InstallOS(COS *os){ os->Run();}
};

int main(int argc, char *argv[])
{
    COS *os = new CLinuxOS();
    CComputer *cmptr = new CIBMComputer();
    cmptr->InstallOS(os);
    delete cmptr;
    cmptr = NULL;
    delete os;
    os = NULL;
    return 0;
}

 

 

第二个示例:

#ifndef SOLUTION_H
#define SOLUTION_H

struct CImplement;
class CAbstractSample
{
public:
    CAbstractSample();
    virtual ~CAbstractSample();
    void abstractDo();
private:
    CImplement *m_impl;
};

#endif // SOLUTION_H
solution.h

 

#include "solution.h"
#include <iostream>
using namespace std;
struct CImplement{
    void doSomething(){
        cout << "CImplement doSomething()" << endl;
    }
};

CAbstractSample::CAbstractSample():m_impl(new CImplement)
{
    cout << "CAbstractSample()" << endl;
}

CAbstractSample::~CAbstractSample()
{
    if(m_impl != NULL)
    {
        delete m_impl;
        m_impl = NULL;
    }
    cout << "~CAbstractSample()" << endl;
}

void CAbstractSample::abstractDo(){
    m_impl->doSomething();
}
solution.cpp
#include "solution.h"
#include <iostream>
using namespace std;
struct CImplement{
    void doSomething(){
        cout << "CImplement doSomething()" << endl;
    }
};

CAbstractSample::CAbstractSample():m_impl(new CImplement)
{
    cout << "CAbstractSample()" << endl;
}

CAbstractSample::~CAbstractSample()
{
    if(m_impl != NULL)
    {
        delete m_impl;
        m_impl = NULL;
    }
    cout << "~CAbstractSample()" << endl;
}

void CAbstractSample::abstractDo(){
    m_impl->doSomething();
}

int main(int argc, char *argv[])
{
    CAbstractSample *as = new CAbstractSample();
    as->abstractDo();
    delete as;
    as = NULL;
    return 0;
}

 

$ g++ -g -Wall -std=c++0x solution.cpp main.cpp -o xmain

posted @ 2017-11-20 19:48  PKICA  阅读(541)  评论(0编辑  收藏  举报