在提出桥梁模式的时候指出,桥梁模式的用意是"将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化"。这句话有三个关键词,也就是抽象化、实现化和脱耦。
创新互联主营富川网站建设的网络公司,主营网站建设方案,成都App定制开发,富川h5小程序定制开发搭建,富川网站营销推广欢迎富川等地区企业咨询
将两个角色之间的继承关系改为聚合关系,就是将它们之间的强关联改换成为弱关联。因此,桥梁模式中的所谓脱耦,就是指在一个软件系统的抽象化和实现化之间使用组合/聚合关系而不是继承关系,从而使两者可以相对独立地变化。这就是桥梁模式的用意。
在使用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; }
网站栏目:桥接模式
文章网址:http://cdxtjz.cn/article/pdjjhe.html