The bridge pattern of C + + implementation of design pattern

Source: Internet
Author: User

Bridge mode, its role is to let the abstraction and implementation of separation, so that both can be changed.

For example, paint, I can draw rectangles, circles, triangles and so on, where to draw? I can draw on the PDF, or I can draw it on Doc. What pictures and where to draw can be independently changed, this kind of situation is more suitable for bridge mode. It means that there are more than one dimensional change in the design and we can use the bridge mode. If only one dimension is changing, then we can solve the problem satisfactorily with inheritance.

My graphic 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 graphics inherit:

#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 do you do it in the picture? I first define a shape's implementation class:

#pragma once  
#include <vector>  
class Impshape  
{public  
:  
    impshape (void);  
    Virtual ~impshape (void);  
      
Public:  
    virtual void Draw (std::vector<point>&);  

Then let the PDF and Doc's implementation classes inherit 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);  

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.