桥接模式

在提出桥梁模式的时候指出,桥梁模式的用意是"将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化"。这句话有三个关键词,也就是抽象化、实现化和脱耦。

创新互联主营富川网站建设的网络公司,主营网站建设方案,成都App定制开发,富川h5小程序定制开发搭建,富川网站营销推广欢迎富川等地区企业咨询

抽象化

存在于多个实体中的共同的概念性联系,就是抽象化。作为一个过程,抽象化就是忽略一些信息,从而把不同的实体当做同样的实体对待【LISKOV94】。

实现化

抽象化给出的具体实现,就是实现化。

脱耦

所谓耦合,就是两个实体的行为的某种强关联。而将它们的强关联去掉,就是耦合的解脱,或称脱耦。在这里,脱耦是指将抽象化和实现化之间的耦合解脱开,或者说是将它们之间的强关联改换成弱关联。

将两个角色之间的继承关系改为聚合关系,就是将它们之间的强关联改换成为弱关联。因此,桥梁模式中的所谓脱耦,就是指在一个软件系统的抽象化和实现化之间使用组合/聚合关系而不是继承关系,从而使两者可以相对独立地变化。这就是桥梁模式的用意。

桥接模式

在使用VIM 编写makefile时,有时TAP键会是八个空格,使用set list命令可以查看所有的TAB键是否正确。

makefile文件

aLL:Brige
CC = g++
INCLUDE = .
CFLAGS = -g -Wall
Brige:main.o CDrawLine.o CDrawSquare.o CSqureRefined.o CCircleRefined.o
        $(CC) -o Brige main.o CDrawLine.o CDrawSquare.o CSqureRefined.o CCircleRefined.o
CDrawSquare.o:CDrawSquare.cpp CDrawSquare.h  CImplement.cpp
        $(CC) -I$(INCLUDE) $(CFLAGS) -c CDrawSquare.cpp
CDrawLine.o:CDrawLine.cpp CDrawLine.h
        $(CC) -I$(INCLUDE) $(CFLAGS) -c CDrawLine.cpp
CSqureRefined.o:CSqureRefined.cpp CSqureRefined.h
        $(CC) -I$(INCLUDE) $(CFLAGS) -c CSqureRefined.cpp
CCircleRefined.o:CCircleRefined.cpp CCircleRefined.h
        $(CC) -I$(INCLUDE) $(CFLAGS) -c CCircleRefined.cpp
main.o:main.cpp
        $(CC) -I$(INCLUDE) $(CFLAGS) -c main.cpp
clean:
 rm *.o
        rm Brige

CAbstraction.h

#ifndef CAbstractiongH
#define CAbstractiongH
#include
#include "CImplement.h"
using namespace std;
class CAbstractiong
{
public:
      virtual  ~CAbstractiong(){};
      virtual void Operation() = 0;
      void SetImplement(CImplement *tImplement){m_CImplement = tImplement;};
protected:
      CAbstractiong(){};
      CImplement *m_CImplement;
};
#endif

CCircleRefined

#ifndef CCircleRefinedH
#define CCircleRefinedH
#include "CAbstraction.h"
class CCircleRefined:public CAbstractiong
{
public:
       CCircleRefined(){};
      ~CCircleRefined(){};
       void  Operation();
                                      
};
#endif
#include "CCircleRefined.h"
void  CCircleRefined::Operation()
{
  cout<<"对圆形进行了"<Operation()<

CSqureRefined

#ifndef CSqureRefinedH
#define CSqureRefinedH
#include "CAbstraction.h"
class CSqureRefined:public CAbstractiong
{
public:
       CSqureRefined(){};
      ~CSqureRefined(){};
       void  Operation();
                               
};
#endif
#include "CSqureRefined.h"
void  CSqureRefined::Operation()
{
  cout<<"对正方形进行了"<Operation()<

CAbstractiong

#ifndef CAbstractiongH
#define CAbstractiongH
#include
#include "CImplement.h"
using namespace std;
class CAbstractiong
{
public:
      virtual  ~CAbstractiong(){};
      virtual void Operation() = 0;
      void SetImplement(CImplement *tImplement){m_CImplement = tImplement;};
protected:
      CAbstractiong(){};
      CImplement *m_CImplement;
};
#endif

CDrawSquare

#ifndef CDrawSquareH
#define CDrawSquareH
#include
#include "CImplement.h"
class CDrawSquare:public CImplement
{
public:
       CDrawSquare(){};
       ~CDrawSquare(){};
      string Operation();
                
};
#endif
#include "CDrawSquare.h"
#include 
using namespace std;
string CDrawSquare::Operation()
{
   string sTemp ="进行了画方格操作";
   return sTemp;
}

CDrawLine

#ifndef CDrawLineH
#define CDrawLineH
#include
#include 
#include "CImplement.h"
class CDrawLine:public CImplement
{
public:
       CDrawLine(){};
       ~CDrawLine(){};
      string Operation();
         
};
#endif
#include "CDrawLine.h"
#include 
using namespace std;
string CDrawLine::Operation()
{
   string sTemp = "进行了画线操作";
   return sTemp;
}

main.cpp

#include "CDrawLine.h"
#include "CDrawSquare.h"
#include "CSqureRefined.h"
#include "CCircleRefined.h"
int main()
{
  CDrawLine *pCDrawLine = new CDrawLine();
  CCircleRefined *pCCircleRefined = new CCircleRefined();
  pCCircleRefined->SetImplement(pCDrawLine);
  pCCircleRefined->Operation();
      
  CDrawSquare *pCDrawSquare = new CDrawSquare();
   pCCircleRefined->SetImplement(pCDrawSquare);
  pCCircleRefined->Operation();
  return 0;
}

本文名称:桥接模式
标题URL:http://myzitong.com/article/pdjjhe.html