Design Pattern simple code Bridge pattern (circular image system design)

Source: Internet
Author: User

/*************************************** **************************************** *****************************/
* Author: Yi Yutian (http://blog.csdn.net/dylgsy ). This article can be ressed casually, but please keep this information
*
* Bridge Mode: separates abstract parts from their implementations so that they can all change independently.
* This sentence is really obscure. Let me explain it. The so-called implementation part can be imagined as a function library, which is organized by classes,
* And implements the functions you need. The abstract part is the class that uses this library.
* Independent changes mean that the code changes for adding or using libraries are independent changes on both sides without affecting the other side.
* It's like a bridge. If you understand it, you will feel that the bridge name has changed really well.
* Well, it doesn't matter if you don't understand it. Let's take a look at the following code. Should be clear
/*************************************** **************************************** *****************************/

/*************************************** **************************************** *****************************/
* Task Description: to implement a drawing system, the requirements of this system are as follows:
* 1. Draw circles and rectangles. (Draw the two shapes first)
* 2. There are already two image galleries, which define the operations we need and we do not need to implement them again.
* 3. Our system can decide which image library to use
* Based on the above requirements, our code is designed as follows (here the bridge mode is not used for comparison ):
/*************************************** **************************************** *****************************/

/*************************************** **************************************** ***************************/
* Note: a precompiled macro _ bridge_mode is used to separate the two pieces of code: bridge and bridge. We can use this to compare the boundaries.
* Two-part code
/*************************************** **************************************** ***************************/

# Include <stdio. h>
# Include <iostream>
Using namespace STD;

// Comment out the code that does not use the bridge mode.
# DEFINE _ bridge_mode

// Image gallery 1
Class cdrawlib1
{
Public:
Void drawcircle ()
{
Cout <"Circle Operation 1" <Endl;
}

Void drawrectangle ()
{
Cout <"draw a rectangle operation 1" <Endl;
}

Void drawtriangle ()
{
Cout <"Triangle draw operation 1" <Endl;
}
};

// Image gallery 2
Class cdrawlib2
{
Public:
Void drawcircle ()
{
Cout <"Circle Operation 2" <Endl;
}
 
Void drawrectangle ()
{
Cout <"rectangular image operation 2" <Endl;
}

Void drawtriangle ()
{
Cout <"Triangle draw operation 2" <Endl;
}
};

// You only need to declare _ bridge_mode to switch between or without the bridge mode.
# Ifndef _ bridge_mode

// When you see the shape, you can easily think of the classic shape abstract class and define a draw interface.
// Here 1 indicates the image painting operation in image gallery 1.
Class cshape1
{
Public:
Virtual void draw () = 0;

Protected:
Cdrawlib1 _ drawlib;
};

// Draw circles and rectangles.
Class ccircle1: Public cshape1
{
Public:
// Implement the draw operation. Use image gallery 1.
Virtual void draw ()
{
// Use Image Library 1
_ Drawlib. drawcircle ();
}
};

Class crectangle1: Public cshape1
{
Public:
// Implement the draw operation, but use Image Library 1
Virtual void draw ()
{
// Use Image Library 1
_ Drawlib. drawrectangle ();
}
};

// Define the shape of image library 2.
Class cshape2
{
Public:
Virtual void draw () = 0;

Protected:
Cdrawlib2 _ drawlib;
};

// Draw circles and rectangles.
Class ccircle2: Public cshape2
{
Public:
// Implement the draw operation. Use Image Library 2.
Virtual void draw ()
{
// Use Image Library 2
_ Drawlib. drawcircle ();
}
};

Class crectangle2: Public cshape2
{
Public:
// Implement the draw operation, but use image library 2
Virtual void draw ()
{
// Use Image Library 2
_ Drawlib. drawrectangle ();
}
};

Void draw1 (cshape1 & S)
{
S. Draw ();
}

Void draw2 (cshape2 & S)
{
S. Draw ();
}

// Well, we started to use the above class to implement our drawing system.
Void main ()
{
Cout <"do not use Bridge Mode" <Endl;
 
// I can use two types of Libraries
Ccircle1 C1;
Ccircle2 C2;
Crectangle1 R1;
Crectangle2 R2;

Draw1 (C1 );
Draw1 (R1 );
Draw2 (C2 );
Draw2 (R2 );
}
/*************************************** **************************************** *****************************/
* Okay. The above code runs normally. If maintenance is not required, we don't have to worry about pulling it ~~
* However, the Code must be maintained and cannot be escaped.
* The change may be like this:
* 1. The third database appears.
* 2. The drawing system needs to draw triangles.
* At this time, let's look at the changes we need to make to complete these two changes, and we will find that I am dizzy.
* (When a programmer is dizzy, that is, the bug is about to appear)
/*************************************** **************************************** *****************************/

# Else

/*************************************** **************************************** *****************************/
* Well, now let's use the bridge mode to implement the above system
* The Bridge Mode separates representation from implementation.
/*************************************** **************************************** *****************************/

// Create a class to implement the operation: cshapeimp
Class cshapeimp
{
Public:
Virtual void drawcircle () = 0;
Virtual void drawrectangle () = 0;
};

Class cshapeimp1: Public cshapeimp
{
Public:
Virtual void drawcircle ()
{
_ Dlib. drawcircle ();
}
Virtual void drawrectangle ()
{
_ Dlib. drawrectangle ();
}

PRIVATE:
Cdrawlib1 _ dlib;
};

Class cshapeimp2: Public cshapeimp
{
Public:
Virtual void drawcircle ()
{
_ Dlib. drawcircle ();
}
Virtual void drawrectangle ()
{
_ Dlib. drawrectangle ();
}
PRIVATE:
Cdrawlib2 _ dlib;
};

Class cshape
{
Public:
Virtual void draw () = 0;

Protected:
Cshapeimp * _ simp;
};

Class ccircle: Public cshape
{
Public:
Ccircle (cshapeimp * IMP)
{
_ Simp = imp;
}
Virtual void draw ()
{
_ Simp-> drawcircle ();
}
};

Class crectangle: Public cshape
{
Public:
Crectangle (cshapeimp * IMP)
{
_ Simp = imp;
}
Virtual void draw ()
{
_ Simp-> drawrectangle ();
}
};

// Well, we can use the above class structure to implement our drawing system.
Void main ()
{
Cout <"use Bridge Mode" <Endl;

Cshapeimp1 simp1;
Cshapeimp2 simp2;

 
// Use Image Library 1
Ccircle C1 (& simp1 );
Crectangle r1 (& simp1 );
C1.draw ();
R1.draw ();

 
// Use Image Library 2
Ccircle C2 (& simp2 );
Crectangle R2 (& simp2 );
C2.draw ();
R2.draw ();
}

/*************************************** **************************************** *****************************/
* Okay. The above code runs normally and is easy to maintain. Let's look back at the two new requirements above:
* 1. The third database appears.
* 2. The drawing system needs to draw triangles.
* For the first requirement, we only need to add another shapeimp.
* Looking at the second requirement, we only need to derive a ctriangle.
* As you can see, changes do not cause confusion. You only need to modify the code separately.
* That is, the changes are separated by bridge.
/*************************************** **************************************** *****************************/

/*************************************** **************************************** *****************************/
* For this model, I personally recommend writing code by myself, and feel the difference between using bridge and not using it,
* It should be done soon.
/*************************************** **************************************** *****************************/

# Endif

 

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.