[构造型模式] 桥接模式的理解
[结构型模式] 桥接模式的理解
![[构造型模式] 桥接模式的理解 [构造型模式] 桥接模式的理解](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDE2LzA1LzE4LzE3MjEwMDE2MzQuanBn)
![[构造型模式] 桥接模式的理解 [构造型模式] 桥接模式的理解](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDE2LzA1LzE4LzE3MjEwMDE2MzUuanBn)
头文件
实现
客户端
运行结果
![[构造型模式] 桥接模式的理解 [构造型模式] 桥接模式的理解](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDE2LzA1LzE4LzE3MjEwMDE2MzYuanBn)
头文件
//BridgePattern.h #ifndef BRIDGEPATTERN_H #define BRIDGEPATTERN_H #include <Windows.h> namespace BridgePattern { // Base Class ////////////////////////////////////////////////////////////////////////// class Implementor { public: Implementor(); virtual ~Implementor(); virtual void OperationImpl() = 0; }; class ConcreteImplementorA : public Implementor { public: ConcreteImplementorA(); virtual ~ConcreteImplementorA(); virtual void OperationImpl(); }; class ConcreteImplementorB : public Implementor { public: ConcreteImplementorB(); virtual ~ConcreteImplementorB(); virtual void OperationImpl(); }; // Base Class ////////////////////////////////////////////////////////////////////////// class Abstraction { public: Abstraction(); virtual ~Abstraction(); virtual void Operation() = 0; }; class ConcreteAbstractionA : public Abstraction { public: ConcreteAbstractionA(Implementor* pImplementor); virtual ~ConcreteAbstractionA(); virtual void Operation(); private: Implementor* m_pImplementor; }; class ConcreteAbstractionB : public Abstraction { public: ConcreteAbstractionB(Implementor* pImplementor); virtual ~ConcreteAbstractionB(); virtual void Operation(); private: Implementor* m_pImplementor; }; ////////////////////////////////////////////////////////////////////////// void BridgePattern_Test_A(); void BridgePattern_Test_B(); } #endif
实现
#include "BridgePattern.h" #include <iostream> using namespace std; namespace BridgePattern { // Base Class ////////////////////////////////////////////////////////////////////////// Implementor::Implementor() { } Implementor::~Implementor() { } ConcreteImplementorA::ConcreteImplementorA() { } ConcreteImplementorA::~ConcreteImplementorA() { } void ConcreteImplementorA::OperationImpl() { cout << "ConcreteImplementorA::OperationImpl\n"; } ConcreteImplementorB::ConcreteImplementorB() { } ConcreteImplementorB::~ConcreteImplementorB() { } void ConcreteImplementorB::OperationImpl() { cout << "ConcreteImplementorB::OperationImpl\n"; } // Base Class ////////////////////////////////////////////////////////////////////////// Abstraction::Abstraction( ) { } Abstraction::~Abstraction() { } ConcreteAbstractionA::ConcreteAbstractionA(Implementor* pImplementor) : m_pImplementor(NULL) { if (NULL == m_pImplementor) { m_pImplementor = pImplementor; } } ConcreteAbstractionA::~ConcreteAbstractionA() { if (m_pImplementor != NULL) { delete m_pImplementor; m_pImplementor = NULL; } } void ConcreteAbstractionA::Operation() { m_pImplementor->OperationImpl(); cout << "ConcreteAbstractionA::Operation\n"; } ConcreteAbstractionB::ConcreteAbstractionB(Implementor* pImplementor) : m_pImplementor(NULL) { if (NULL == m_pImplementor) { m_pImplementor = pImplementor; } } ConcreteAbstractionB::~ConcreteAbstractionB() { if (m_pImplementor != NULL) { delete m_pImplementor; m_pImplementor = NULL; } } void ConcreteAbstractionB::Operation() { m_pImplementor->OperationImpl(); cout << "ConcreteAbstractionB::Operation\n"; } ////////////////////////////////////////////////////////////////////////// void BridgePattern_Test_A() { ConcreteAbstractionA* pAbstraction = new ConcreteAbstractionA(new ConcreteImplementorA()); pAbstraction->Operation(); delete pAbstraction; pAbstraction = NULL; } void BridgePattern_Test_B() { ConcreteAbstractionB* pAbstraction = new ConcreteAbstractionB(new ConcreteImplementorB()); pAbstraction->Operation(); delete pAbstraction; pAbstraction = NULL; } }
客户端
#include "BridgePattern.h" #include <iostream> using namespace std; using namespace BridgePattern; void main() { BridgePattern_Test_A(); BridgePattern_Test_B(); }
运行结果