• Bridge Pattern


    1.Bridge Pattern :使用组合的方式将“抽象”和“实现”彻底的解耦。这里的实现不是指继承基类,实现基类接口,而是指通过对象的组合实现用户的需求。

    面向对象分析和设计中的原则: Favor Compsition Over Inheritance.

    2.Bridge Pattern 模式结构图

    3.实现

     1 #include "Abstraction.h" 
     2 #include "AbstractionImp.h"
     3 #include <iostream> 
     4 using namespace std;
     5 
     6 int main(int argc,char* argv[]) 
     7 { 
     8     AbstractionImp* imp = new ConcreteAbstractionImpA();
     9     Abstraction* abs = new RefinedAbstraction(imp);
    10     abs->Operation();
    11     return 0;
    12 }
    main.cpp
     1 #ifndef _ABSTRACTION_H_ 
     2 #define _ABSTRACTION_H_
     3 class AbstractionImp;
     4 
     5 class Abstraction 
     6 { 
     7 public: 
     8     virtual ~Abstraction();
     9     virtual void Operation() = 0;
    10 protected: 
    11     Abstraction();
    12 private:
    13 };
    14 
    15 class RefinedAbstraction:public Abstraction 
    16 { 
    17 public: 
    18     RefinedAbstraction(AbstractionImp* imp);
    19     ~RefinedAbstraction();
    20     void Operation();
    21 protected:
    22 private: 
    23     AbstractionImp* _imp;
    24 };
    25 
    26 #endif
    Abstraction.h
     1 #include "Abstraction.h" 
     2 #include "AbstractionImp.h"
     3 #include <iostream> 
     4 using namespace std;
     5 
     6 Abstraction::Abstraction() 
     7 {
     8 
     9 }
    10 Abstraction::~Abstraction() 
    11 {
    12 
    13 }
    14 RefinedAbstraction::RefinedAbstraction(AbstractionImp* imp) 
    15 {
    16     _imp = imp;
    17 }
    18 RefinedAbstraction::~RefinedAbstraction()
    19 {
    20 
    21 }
    22 void RefinedAbstraction::Operation() 
    23 { 
    24     _imp->Operation();
    25 }
    Abstraction.cpp
     1 #ifndef _ABSTRACTIONIMP_H_ 
     2 #define _ABSTRACTIONIMP_H_
     3 
     4 class AbstractionImp 
     5 { 
     6 public: 
     7     virtual ~AbstractionImp();
     8     virtual void Operation() = 0;
     9 protected: 
    10     AbstractionImp();
    11 private:
    12 };
    13 
    14 class ConcreteAbstractionImpA:public AbstractionImp 
    15 { 
    16 public: 
    17     ConcreteAbstractionImpA();
    18     ~ConcreteAbstractionImpA();
    19     virtual void Operation();
    20 protected: 
    21 private:
    22 };
    23 
    24 class ConcreteAbstractionImpB:public AbstractionImp 
    25 { 
    26 public:
    27     ConcreteAbstractionImpB();
    28     ~ConcreteAbstractionImpB();
    29     virtual void Operation();
    30 protected: 
    31 private:
    32 };
    33 
    34 #endif
    AbstractionImp.h
     1 #include "AbstractionImp.h"
     2 #include <iostream> 
     3 using namespace std;
     4 
     5 AbstractionImp::AbstractionImp() 
     6 {
     7 
     8 }
     9 AbstractionImp::~AbstractionImp() 
    10 {
    11 
    12 }
    13 void AbstractionImp::Operation() 
    14 { 
    15     cout<<"AbstractionImp....imp..."<<endl;
    16 }
    17 ConcreteAbstractionImpA::ConcreteAbstractionImpA() 
    18 {
    19 
    20 }
    21 ConcreteAbstractionImpA::~ConcreteAbstractionImpA() 
    22 {
    23 
    24 }
    25 void ConcreteAbstractionImpA::Operation() 
    26 { 
    27     cout<<"ConcreteAbstractionImpA...."<<endl;
    28 }
    29 ConcreteAbstractionImpB::ConcreteAbstractionImpB() 
    30 {
    31 
    32 }
    33 ConcreteAbstractionImpB::~ConcreteAbstractionImpB() 
    34 {
    35 
    36 }
    37 void ConcreteAbstractionImpB::Operation() 
    38 { 
    39     cout<<"ConcreteAbstractionImpB...."<<endl;
    40 }
    AbstractionImp.cpp
  • 相关阅读:
    js,jQuery实现可关闭悬浮框广告特效,兼容(谷歌,火狐,Ie)
    各种选择框jQuery的选中方法
    表单校验demo
    两种方法实现城市级联菜单
    树形菜单
    匿名函数和鼠标移入移除事件
    多线程实例
    Lock锁
    Oracle语句
    Java NIO
  • 原文地址:https://www.cnblogs.com/programmer-wfq/p/4655115.html
Copyright © 2020-2023  润新知