标签:c++ 设计模式
桥接模式,将抽象部分与它的实现部分分离,使他们都可以独立的变化。#include "stdafx.h" #include <iostream> using namespace std; class Implement { public: virtual ~Implement(){} virtual void OperationImp() = 0; }; class ConcreteImplementA:public Implement { public: virtual void OperationImp() { cout << "ConcreteImplementA" << endl; } }; class ConcreteImplementB:public Implement { public: virtual void OperationImp() { cout << "ConcreteImplementB" << endl; } }; class Abstraction { public: virtual ~Abstraction() { delete m_pImplement; } virtual void Operation() = 0; void SetImplement(Implement* implement) { m_pImplement = implement; } Implement* GetImplement()const { return m_pImplement; } private: Implement* m_pImplement; }; class RedefinedAbstraction :public Abstraction { public: virtual void Operation() { GetImplement()->OperationImp(); } };
#include "stdafx.h" #include "Bridge.h" int _tmain(int argc, _TCHAR* argv[]) { Abstraction* abstrastion = new RedefinedAbstraction(); abstrastion->SetImplement(new ConcreteImplementA()); abstrastion->Operation(); abstrastion->SetImplement(new ConcreteImplementB()); abstrastion->Operation(); getchar(); return 0; }
标签:c++ 设计模式
原文地址:http://blog.csdn.net/wwwdongzi/article/details/26975475