码迷,mamicode.com
首页 > 其他好文 > 详细

Bridge Pattern

时间:2015-05-24 10:07:50      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

AbstractionImp.h

#ifndef _ABSTRACTIONIMP_H
#define _ABSTRACTIONIMP_H

class AbstractionImp {
public :
    virtual ~AbstractionImp();
    virtual void Operation() = 0;
protected :
    AbstractionImp();
};

class ConcreateAbstractionImpA : public AbstractionImp {
public :
    ConcreateAbstractionImpA();
    ~ConcreateAbstractionImpA();
    virtual void Operation();
};

class ConcreateAbstractionImpB : public AbstractionImp {
public :
    ConcreateAbstractionImpB();
    ~ConcreateAbstractionImpB();
    virtual void Operation();
};

#endif

AbstractionImp.cpp

#include "AbstractionImp.h"
#include <iostream>
using namespace std;

AbstractionImp::AbstractionImp() {}

AbstractionImp::~AbstractionImp() {}

void AbstractionImp::Operation() {
    cout << "AbstractionImp...imp" << endl;
}

ConcreateAbstractionImpA::ConcreateAbstractionImpA() {}

ConcreateAbstractionImpA::~ConcreateAbstractionImpA() {}

void ConcreateAbstractionImpA::Operation() {
    cout << "ConcreateAbstractionImpA..." << endl;
}

ConcreateAbstractionImpB::ConcreateAbstractionImpB() {}

ConcreateAbstractionImpB::~ConcreateAbstractionImpB() {}

void ConcreateAbstractionImpB::Operation() {
    cout << "ConcreateAbstractionImpB..." << endl;
}

Abstraction.h

#ifndef _ABSTRACTION_H
#define _ABSTRACTION_H

class AbstractionImp;

class Abstraction {
public :
    virtual ~Abstraction();
    virtual void Operation() = 0;
protected :
    Abstraction();
};

class RefinedAbstraction : public Abstraction {
public :
    RefinedAbstraction(AbstractionImp *imp);
    ~RefinedAbstraction();
    void Operation();
private :
    AbstractionImp *_imp;
};

#endif

Abstraction.cpp

#include "Abstraction.h"
#include "AbstractionImp.h"
#include <iostream>
using namespace std;

Abstraction::Abstraction() {}

Abstraction::~Abstraction() {}

RefinedAbstraction::RefinedAbstraction(AbstractionImp *imp) {
    _imp = imp;
}

RefinedAbstraction::~RefinedAbstraction() {}

void RefinedAbstraction::Operation() {
    _imp->Operation();
}

Main.cpp

#include "Abstraction.h"
#include "AbstractionImp.h"
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
    
    AbstractionImp *imp = new ConcreateAbstractionImpA();
    Abstraction *abs = new RefinedAbstraction(imp);
    abs->Operation();

    return 0;
}

 

Bridge Pattern

标签:

原文地址:http://www.cnblogs.com/Susake/p/4525355.html

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