preface

Today is the second type of structural design pattern, the bridging pattern. Bridging – refers to the process of forwarding network packets based on the link layer address of the OSI network model, working at layer 2 of the OSI. In general, the network bridge has the function of bridging. This is an encyclopedic explanation. So what is the bridge mode today? Bridging is a link between the Hong Kong-Zhuhai-Macao Bridge in recent years. Acts as the link between the three places. What about bridging in design mode? Similarly, the components are also connected by Bridges and ties. And then assemble it into what you need. Let’s look at what the bridge pattern means in detail.

Describes the bridge mode

A, the other

When we are facing the system program development and design, sometimes we will face the change of two dimensions in some classes. In the case of a variety of possible changes, inheritance will cause more complex program consequences and not so flexible in extension. So how to face the classification of multiple angles and multiple angles may change? This is where the bridge mode comes in.

Second, the intention

Separate the abstract parts from the implementation parts so that they can vary independently.

3. Case diagram

Bridge mode code examples

Looking at the case diagram above, we can know that the bridge mode includes the following parts:

Abstract role: Abstraction gives the definition, including a reference to the implementation object

Concrete Abstract characters: Extend to implement abstract characters

Implement role: This role provides the interface to implement the role, but does not implement it. This role does not have to be the same as the abstract role. The opposite can be completely different. The equivalent of an abstract character corresponds to one dimension, while the realized character corresponds to the second dimension. Implement roles: Extend implementation roles

Let’s look at an example. Everyone knows there are Android and IOS phones. So for software also need to adapt to two types. At the same time, the software is constantly updated. For that matter. For a mobile phone app. Let’s look at two dimensions for a moment. First, iteration updated version. Two, suitable for mobile phone type. If this type of problem is solved by inheritance, it will greatly increase the complexity of the system. Here we can use bridge mode. Let’s take a look at how the code implements its design:

  

namespace Bridge_Pattern
{
    class BridgePattern
    {
    }
    #region Abstract role -- Iteratively updated version (containing references to the implemented role object) ========Public abstract class Version {/// <summary> // implement a reference to the implementation object, /// </summary> protected PhoneType _phoneType; // param name= param name= param name= param name= param name= param name= param name= param name= param name= param name="phoneType"></param> public Version(PhoneType phoneType) { this._phoneType = phoneType; } /// </summary> // </summary> public abstract void Create(); }#endregion

    # region to realize the role - suitable for mobile phone type = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Public abstract class PhoneType {/// <summary> /// </summary> public abstract void SetType(); }#endregion

    #region Concrete Abstract role - concrete actual iteration updated version ===========// </summary> // version 1.0 /// </summary> Public class OneVersion: Version { public OneVersion(PhoneType phoneType) :base(phoneType) { } public override voidCreate()
        {
            Console.WriteLine("Current version 1.0"); this._phoneType.SetType(); // </summary> // </summary> public class TwoVersion: Version { public TwoVersion(PhoneType phoneType) : base(phoneType) { } public override voidCreate()
        {
            Console.WriteLine("Current version 2.0"); this._phoneType.SetType(); }}#endregion

    Region Specifies the role -- the actual use type ===================/// </summary> /// android /// </summary> public class AndroidPhoneType: PhoneType {public override voidSetType()
        {
            Console.WriteLine("Currently Android type"); }} /// <summary> // ios /// </summary> public class IOSPhoneType: PhoneType {public override voidSetType()
        {
            Console.WriteLine("Currently IOS type"); }}#endregion

}Copy the code

Class Program {static void Main(string[] args) {AndroidPhoneType = new AndroidPhoneType(); OneVersion oneVersion = new OneVersion(androidPhoneType); oneVersion.Create(); IOSPhoneType = new IOSPhoneType(); TwoVersion twoVersion = new TwoVersion(iOSPhoneType); twoVersion.Create(); Console.ReadLine(); }}Copy the code

Usage scenarios and advantages and disadvantages

First, use scenarios

1. If the system needs to build some flexibility between abstract and implementable roles, avoid the form of inherited classes. You can use the bridge mode to establish associations

2, if the system does not want to use inheritance form or inheritance form will cause the system to be extremely complex, consider using bridge mode

3. If a class has two dimensions, and both dimensions are constantly changing and expanding, using the bridge pattern is best

Second, the advantages of

1. Decouple the abstract interface from the implementation

2, abstraction and implementation are separated from each other, easy to expand

3. Implementation details are transparent to users

Three and disadvantages

1. Increased the difficulty of understanding and designing the system

conclusion

Bridge mode. It means what it says. It is the separation of two dimensions in a class. Decouple the abstract interface from the implementation. They are then linked together via composite Bridges. Use in combination. Both objects are abstractions, and then subclasses are concrete practices. Associated by an abstract interface reference to an object that implements the role. The bridging pattern is suitable for classes where two dimensions change frequently. Isolate the angles and let them vary independently, reducing coupling.

No matter how high the sky is, standing on tiptoe is closer to the sun.