大话设计模式--桥接模式 Bridge -- C++实现实例
2013-10-16 11:17
453 查看
1. 桥接模式: 将抽象部分与它的实现部分分离,使它们都可以独立的变化。
分离是指 抽象类和它的派生类用来实现自己的对象分离。
![](http://img.blog.csdn.net/20131016110734562)
实现系统可以有多角度分类,每一种分类都有可能变化,那么把这种多角度分离出来让他们独立变化,减少他们之间的耦合。
![](http://img.blog.csdn.net/20131016110631546)
实例:
implementor.h implementor.cpp 实现接口
concreteimplementora.h concreteimplementora.cpp 具体实现功能A
concreteimplementorb concreteimplementorb 具体实现功能B
abstraction.h abstraction.cpp 抽象
refinedabstraction.h refinedabstraction.cpp 精确抽象
main.cpp
分离是指 抽象类和它的派生类用来实现自己的对象分离。
实现系统可以有多角度分类,每一种分类都有可能变化,那么把这种多角度分离出来让他们独立变化,减少他们之间的耦合。
实例:
implementor.h implementor.cpp 实现接口
#ifndef IMPLEMENTOR_H #define IMPLEMENTOR_H class Implementor { public: Implementor(); void virtual operation()=0; }; #endif // IMPLEMENTOR_H
#include "implementor.h" Implementor::Implementor() { }
concreteimplementora.h concreteimplementora.cpp 具体实现功能A
#ifndef CONCRETEIMPLEMENTORA_H #define CONCRETEIMPLEMENTORA_H #include "implementor.h" class ConcreteImplementorA : public Implementor { public: ConcreteImplementorA(); void operation(); }; #endif // CONCRETEIMPLEMENTORA_H
#include "concreteimplementora.h" #include <stdio.h> ConcreteImplementorA::ConcreteImplementorA() { } void ConcreteImplementorA::operation() { printf("ConcreteImplementorA operation\n"); }
concreteimplementorb concreteimplementorb 具体实现功能B
#ifndef CONCRETEIMPLEMENTORB_H #define CONCRETEIMPLEMENTORB_H #include "implementor.h" class ConcreteImplementorB : public Implementor { public: ConcreteImplementorB(); void operation(); }; #endif // CONCRETEIMPLEMENTORB_H
#include "concreteimplementorb.h" #include <stdio.h> ConcreteImplementorB::ConcreteImplementorB() { } void ConcreteImplementorB::operation() { printf("ConcreteImplementorB operation\n"); }
abstraction.h abstraction.cpp 抽象
#ifndef ABSTRACTION_H #define ABSTRACTION_H #include "implementor.h" class Abstraction { public: Abstraction(); void setImplementor(Implementor *implementor); void virtual operation(); protected: Implementor *implementor; }; #endif // ABSTRACTION_H
#include "abstraction.h" Abstraction::Abstraction() { implementor = 0; } void Abstraction::setImplementor(Implementor *implementor) { this->implementor = implementor; } void Abstraction::operation() { implementor->operation(); }
refinedabstraction.h refinedabstraction.cpp 精确抽象
#ifndef REFINEDABSTRACTION_H #define REFINEDABSTRACTION_H #include "abstraction.h" class RefinedAbstraction : public Abstraction { public: RefinedAbstraction(); void operation(); }; #endif // REFINEDABSTRACTION_H
#include "refinedabstraction.h" RefinedAbstraction::RefinedAbstraction() { } void RefinedAbstraction::operation() { implementor->operation(); }
main.cpp
#include <iostream> #include "refinedabstraction.h" #include "concreteimplementora.h" #include "concreteimplementorb.h" using namespace std; int main() { cout << "Bridge test!" << endl; Abstraction *ab = new RefinedAbstraction(); ab->setImplementor(new ConcreteImplementorA()); ab->operation(); ab->setImplementor(new ConcreteImplementorB()); ab->operation(); return 0; }
相关文章推荐
- 大话设计模式--桥接模式 Bridge -- C++实现实例
- 大话设计模式--状态模式 State -- C++实现实例
- 大话设计模式--命令模式 Command -- C++实现实例
- 大话设计模式--职责连模式 Chain of Resposibility -- C++实现实例
- 大话设计模式--外观模式 Facade -- C++实现实例
- 大话设计模式--职责连模式 Chain of Resposibility -- C++实现实例
- 大话设计模式--命令模式 Command -- C++实现实例
- 大话设计模式--外观模式 Facade -- C++实现实例
- 大话设计模式--建造者模式 Builder -- C++实现实例
- 大话设计模式--建造者模式 Builder -- C++实现实例
- 大话设计模式_桥接模式c++实现
- 大话设计模式--观察者模式 Observer -- C++ 实现实例
- 大话设计模式--策略模式 strategy -- C++实现实例
- 大话设计模式--观察者模式 Observer -- C++ 实现实例
- 大话设计模式--策略模式 strategy -- C++实现实例
- 大话设计模式--工厂模式 factory -- C++实现实例
- 大话设计模式--中介者模式 Mediator --C++实现实例
- 大话设计模式--工厂模式 factory -- C++实现实例
- 大话设计模式--组合模式 Composite -- C++实现实例
- 大话设计模式--中介者模式 Mediator --C++实现实例