I understand the design pattern (C ++ implementation) -- Bridge pattern)

Source: Internet
Author: User

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);}
  • It is easy to use:
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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.