Step by step. NET Design Mode Study Notes 13, Bridge (Bridge Mode)

Source: Internet
Author: User

Overview

In software systems, some types of software systems have two or more dimensional changes due to their own logic. How can we deal with such "multi-dimensional changes "? How can we use object-oriented technology to make this type easily change in multiple directions without introducing additional complexity? This requires the Bridge mode.

The bridge mode is a very useful and complex mode. Familiarity with this mode is helpful for understanding the object-oriented design principles, including the "open-closed" principle (OCP) and the combination/aggregation Reuse Principle (CARP. Understanding these two principles helps to form correct design ideas and cultivate a good design style.

Intention

Separate the abstract part from the implementation part so that they can all change independently. [GOF "design patterns"] The abstraction and implementation here are not necessarily at the same level. For example, database operations can be attributed to "adding, deleting, and modifying ". Many business processes are implemented through database operations. For example, in "Inventory Management", "warehouse receiving", the software implementation of this business action can be described as "adding a record to the inventory table ", the "warehouse receiving" and "insert record" are at different service levels.

<Design Pattern> structure chart

Figure 1 Bridge Mode Structure

We can see that this system has two levels, namely:

  • Abstract level structure composed of abstract roles and corrected abstract roles.
  • The implementation level structure consists of an implementation role and two specific implementation roles.

The following roles are involved in the Bridge Mode:

  • Abstract action role: defines the abstract object and saves a reference to the implemented object.
  • Modify abstract roles: Expand abstract roles, and modify the abstract definitions of the parent class.
  • Implementor role: this role provides an interface for implementing the role, but does not provide a specific implementation. It must be noted that this interface is not necessarily the same as the interface definition of abstract roles. In fact, these two interfaces can be very different. An implemented role should only provide underlying operations, while an abstract role should only provide operations at a higher level based on underlying operations.
  • Concrete Implementor role: this role provides the specific implementation of the role interface.

Examples in life

The Bridge Mode separates the abstract part from its implementation so that they can change independently. A common switch-controlled electric lamp or fan is an example of bridging. The purpose of the switch is to turn the device on or off. The actual switch can be a simple double-knife zipper switch or a dimming switch.

Figure 2 bridging object graph using an electronic switch example

 

Image metaphor

When we were young, we painted with crayons, a box of 12 colors. At first, I used the minimum number of wax strokes to draw a sun male, and the Moon mother-in-law was enough. Later, I began to draw some abstract works, so I had to change the medium size. Otherwise, I would have to draw a background for half a day. A box of medium size would also contain 12 colors. Then I started to turn to the luxury School, and the medium size was a little too small, so I had to change the large size, so there were only 12 colors in a box. You see, a Paster like me needs 36 paint brushes, which is too troublesome. However, I have observed that there are not so many painters who are more famous than me. They only have a few brushes and some paint, which solves the problem of "type explosion" of crayons. As shown in:


I want to use 36 crayons

Mr. Qi Baishi only uses 3 kinds of brush and 12 kinds of paint.

 

Example

Which type of control program is used to enable and disable the control program? For example, TV, lamp, etc. The bridge mode is exactly the same, as shown in the following figure:

Code Design

Create CntrlControl. cs First:

 
01 /// <summary>
02 /// control class
03 /// </summary>
04 public abstract class CntrlControl 
05 {
06     /// <summary>
07     /// turn on
08     /// </summary>
09     /// <returns></returns>
10     public abstract string TurnOn();
11  
12     /// <summary>
13     /// turn off
14     /// </summary>
15     /// <returns></returns>
16
Related Article

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.