7. Bridging mode (bridge pattern)

Source: Internet
Author: User

Motive (motivate):
In a software system, some types, because of their own logic, have two or more dimensions that change, so how do you deal with this "multidimensional change"? How can you use object-oriented technology to make this type easily change in multiple directions without introducing additional complexity?

Intention (Intent):
Separate the abstractions from the implementation, so that they can be changed independently.
------"Design Mode" GOF
Structure diagram (struct):

      


Examples of life:

I think we all have the experience of crayon painting when we were young. A large box of delight's crayons depicts the format pattern according to the imagination. And the brush under the Chinese painting is meticulous freehand brushwork, each exhibition style. Today, our story starts with a crayon and a brush.

Imagine to draw a picture, blue sky, white clouds, green trees, birds, if the screen size is large, then use crayons to draw a little trouble. After all, thin crayons to paint a blue sky, is a bit of trouble. If possible, it is best to have a large set of crayons, coarse crayons can be applied quickly. As for color, the best color to be thick, in addition to the blue sky and green space. In this way, if a set of 12 colors of crayons, we need two sets of 24, the same color of a thick and thin. Oh, the painting has not been painted, began to dream: if a set of medium crayon is better, so, not a lot of a total of 36 crayons.

Then look at this side of the brush, incredibly simple: a set of watercolor 12 colors, plus a large middle and small three brush. You can not underestimate this "primitive" combination, painting the blue sky with a large brush, painting small birds with a little brush, each with a special feature.

Oh, you are not already looked out, good, I want to say today is bridge mode. For a painting, we need to prepare 36 different types of crayons, and the use of brush three is enough, of course, with 12 kinds of pigments. Through bridge mode, we change the multiplication operation 3x12=36 to the addition Operation 3+12=15, which is not a small improvement. So what's the difference between a crayon and a brush?

In fact, the key difference between a crayon and a brush is whether the pen and color can be separated. The "GOF95" bridge model is intended to "decouple abstraction (abstraction) from implementation (implementation) so that they can vary independently". The key is whether to decouple. The color of crayons and crayons is inseparable, so it is necessary to use 36 colors, different sizes of crayons to draw pictures. and brushes and pigments can be very good decoupling, independent changes, it simplifies the operation. Here, the concept of abstraction is: "Brush paint with paint", and in the realization, the brush has a large medium and small third, the pigment has red and green blue and other 12 kinds, so it can appear 3x12 kind of combination. Each participant (brush and paint) can switch freely on his or her own degree of freedom.

Crayon because the pen and color can not be separated, resulting in pen and color two degrees of freedom cannot be changed individually, so that only 36 objects can be created to complete the task. Bridge mode transforms the inheritance relationship into a composite relationship, which reduces the coupling between systems and reduces the amount of code written.

Code implementation:

 public  abstract  class   Brush { protected   Color C;  public  abstract  void   Paint ();  public  void   SetColor (color c) { this . C = C; }} 


//bigbrush
    class Bigbrush:brush    {        publicoverridevoid  Paint ()        {            Console.WriteLine ("  Using Big brush and color {0} painting", C.color);        }    }

Smallbrush

    class Smallbrush:brush    {        publicoverridevoid  Paint ()        {            Console.WriteLine ("  Using Small brush and color {0} painting", C.color);        }    }

    class Color    {        publicstring  color;    }

Red
    class Red:color    {        public  Red ()        {            this"red";        }    }

Green

    class Green:color    {        public  Green ()        {            this"green" ;        }    }

Blue

    class Blue:color    {        public  Blue ()        {            this"blue";        }    }

1 class Program
2 {
3 public static void Main ()
4 {
5 Brush B = new Bigbrush ();
6 B.setcolor (New Red ());
7 B.paint ();
8 B.setcolor (New Blue ());
9 B.paint ();
B.setcolor (New Green ());
B.paint ();
12
b = new Smallbrush ();
B.setcolor (New Red ());
B.paint ();
B.setcolor (New Blue ());
B.paint ();
B.setcolor (New Green ());
B.paint ();
20}


Applicability:
1. If a system needs to add more flexibility between the abstract and materialized roles of the artifacts, avoid establishing a static connection between the two levels.

2. Design requires that any change to the implementation role should not affect the client, or that the change to the implementation role is completely transparent to the client.

3. A component has more than one abstract role and an implementation role, and the system requires dynamic coupling between them.

4. While there is no problem with using inheritance in the system, the design requirements need to be managed independently because the abstract and materialized roles need to change independently.

Bridge Essentials:
1. Bridge mode uses a "composite relationship between objects" to decouple the inherent binding between abstractions and implementations, allowing abstractions and implementations to evolve along their respective dimensions.

2. The so-called abstraction and implementation along the respective dimensions of the change, namely "sub-class" them, to obtain the sub-classes, they can be arbitrary, thereby obtaining different models on different platforms.

3. Bridge mode is sometimes similar to multi-inheritance scenarios, but multiple inheritance schemes often violate the single-class principle of responsibility (that is, one class has only one change), and reusability is poor. Bridge mode is a better solution than a multi-inheritance scheme.

4. Bridge mode is generally used in "two very strong change dimensions", sometimes even with two changing dimensions, but the dimension of change in one direction is not dramatic-in other words, two changes do not result in criss-crossing results, and bridge mode is not necessarily used.

7. Bridging mode (bridge pattern)

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.