标签:
1.意图
将抽象部分与它的实现部分分离,使它们都可以独立地变化。
2.动机
在抽象类与它的实现之间起到桥梁作用,使它们可以独立地变化。
3.适用性
4.结构
5.代码实例
#include <memory> class AbstactionImpl; class Abstration { public: void virtual Operaion()=0; void virtual SetImpl(std::shared_ptr<AbstactionImpl>& pImpl)=0; protected: std::shared_ptr<AbstactionImpl> m_pImpl; }; class RedefinedAbstration : public Abstration { public: void Operaion(); void SetImpl(std::shared_ptr<AbstactionImpl>& pImpl); };
class AbstactionImpl { public: void virtual OperationImpl()=0; }; class ConcreteImplentorA : public AbstactionImpl { public: void OperationImpl(); }; class ConcreteImplentorB : public AbstactionImpl { public: void OperationImpl(); };
#include "Abstration.h" #include "Implentor.h" void RedefinedAbstration::Operaion() { m_pImpl->OperationImpl(); } void RedefinedAbstration::SetImpl(std::shared_ptr<AbstactionImpl>& pImpl) { m_pImpl = pImpl; }
#include <iostream> #include "Implentor.h" void ConcreteImplentorA::OperationImpl() { std::cout<<"ConcreteImplenorA Exectued" <<std::endl; } void ConcreteImplentorB::OperationImpl() { std::cout<<"ConcreteImplentorB Exectued"<<std::endl; }
#include <memory> #include "Abstration.h" #include "Implentor.h" int main() { std::shared_ptr<AbstactionImpl> pConcreteImplA(new ConcreteImplentorA); std::shared_ptr<AbstactionImpl> pConcreteImplB(new ConcreteImplentorB); std::shared_ptr<RedefinedAbstration> pRedefinedAbstration(new RedefinedAbstration); pRedefinedAbstration->SetImpl(pConcreteImplA); pRedefinedAbstration->Operaion(); pRedefinedAbstration->SetImpl(pConcreteImplB); pRedefinedAbstration->Operaion(); while(1); }
6.测试结果
7.效果
标签:
原文地址:http://www.cnblogs.com/mgp200866130/p/5516866.html