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

c++桥接模式

时间:2017-11-20 21:41:37      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:close   define   rac   发展   color   标准   open   new   csdn   

可以简记为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++桥接模式

标签:close   define   rac   发展   color   标准   open   new   csdn   

原文地址:http://www.cnblogs.com/guxuanqing/p/7867952.html

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