1. 程式人生 > >c++橋接模式

c++橋接模式

sin name private tac linux ibm define stream virt

可以簡記為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

c++橋接模式