The role of the bridge mode is to separate abstraction and implementation so that both of them can change.
For example, where can I draw a rectangle, circle, triangle, and so on? I can draw on PDF or Doc. You can change the image type and the image type independently. In this case, the bridge mode is more suitable. That is to say, we can use the bridge mode when there are more than one dimensional changes in the design. If only one dimension is changing, we can use inheritance to solve the problem satisfactorily.
My graph definition:
#pragma once#include<vector>#include"ImpShape.h"class IShape{public:IShape(void);virtual ~IShape(void);virtual std::vector<Point> getDrawPoints();void paint();public:ImpShape *implementor;};
All other images are inherited:
#pragma once#include "ishape.h"class CRectangle :public IShape{public:CRectangle(void);~CRectangle(void);};#pragma once#include "ishape.h"class CCircle :public IShape{public:CCircle(void);~CCircle(void);};
So how can we achieve this problem? First, I will define an implementation class for shape:
#pragma once#include<vector>class ImpShape{public:ImpShape(void);virtual ~ImpShape(void);public:virtual void draw(std::vector<Point>&);};
So that the implementation classes of PDF and Doc are inherited from impshape:
#pragma once#include "impshape.h"class ImpPdf :public ImpShape{public:ImpPdf(void);~ImpPdf(void);};#pragma once#include "impshape.h"class ImpDoc :public ImpShape{public:ImpDoc(void);~ImpDoc(void);};
Imppdf and impdoc must inherit and override the draw function of impshape.
Our abstraction and implementation are both well implemented. How can we connect them and how can we use them?
- You may have noticed that ishape contains an impshape pointer! Yes. Because impshape implements ishape, including is used here, which can be conveniently called in other functions of ishape.
#include "StdAfx.h"#include "IShape.h"IShape::IShape(void){}IShape::~IShape(void){}void IShape::paint(){std::vector<Point> vpoints = getDrawPoints();this->implementor->draw(vpoints);}
IShape *item = new CCircle();item->implementor = new ImpPdf();item->paint();item = new CRectangle();item->implementor = new ImpDoc();item->paint();
This not only satisfies the changes in the image but also the problem of the image. The class diagrams between them are as follows:
Lcl_data was originally created in csdn. Net [http://blog.csdn.net/lcl_data/article/details/8710134]