在提出桥梁模式的时候指出,桥梁模式的用意是"将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化"。这句话有三个关键词,也就是抽象化、实现化和脱耦。
将两个角色之间的继承关系改为聚合关系,就是将它们之间的强关联改换成为弱关联。因此,桥梁模式中的所谓脱耦,就是指在一个软件系统的抽象化和实现化之间使用组合/聚合关系而不是继承关系,从而使两者可以相对独立地变化。这就是桥梁模式的用意。
在使用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<iostream> #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<<"对圆形进行了"<<m_CImplement->Operation()<<endl; }
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<<"对正方形进行了"<<m_CImplement->Operation()<<endl; }
CAbstractiong
#ifndef CAbstractiongH #define CAbstractiongH #include<iostream> #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<iostream> #include "CImplement.h" class CDrawSquare:public CImplement { public: CDrawSquare(){}; ~CDrawSquare(){}; string Operation(); }; #endif #include "CDrawSquare.h" #include <iostream> using namespace std; string CDrawSquare::Operation() { string sTemp ="进行了画方格操作"; return sTemp; }
CDrawLine
#ifndef CDrawLineH #define CDrawLineH #include<iostream> #include <string> #include "CImplement.h" class CDrawLine:public CImplement { public: CDrawLine(){}; ~CDrawLine(){}; string Operation(); }; #endif #include "CDrawLine.h" #include <iostream> 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; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。