zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    zookeeper 分布式锁
    kafka比较好的文章
    HTTP深入浅出 http请求
    火狐浏览器下event对象的兼容处理
    透明度轮播框架封装注意点zIndex值不能忘记
    wamp的phpmyadmin无法访问Forbidden的解决方案
    同一DIV内,两个行内块元素不对齐的解决方案
    tween.js运动曲线
    什么是闭包?闭包的优缺点?
    firefox和IE9不支持对icon font字体的跨域访问
  • 原文地址:https://www.cnblogs.com/programmer-wfq/p/4655115.html
Copyright © 2011-2022 走看看