C # design mode BA Qiao Yangzhong mode (bridge) "Structural type"

Source: Internet
Author: User

First, Introduction

Today we are going to talk about the second mode of "structural" design pattern, which is "bridge mode", also called "Bridge mode". What do you think the first time you see this name? The first time I saw this pattern, I guess it was connected to something by name. Because the bridge in our real life is often connected to a and B, and then to the later development, Bridge extension as a link, such as: The Silk Road is a bridge connecting Asia and Europe. With the bridge, we travel conveniently, from one place to another in the case of a bridge is more convenient (here is not allowed to contradicting, of course, the need for a bridge). Bridge is aimed at the use of the bridge environment, to solve the problem of crossing and convergence. In the design mode of "bridge mode" also has a similar concept, is connected to two different dimensions of things, and these two dimensions have a strong change, what is called strong, often change, what is often? Haha, I understand it.

Second, the bridge mode detailed introduction

2.1. Motive (motivate)

In many game scenarios, there will be the situation: "equipment" itself will have their own inherent logic, such as firearms, there will be model problems, and now a lot of games in different media platform to run and use, so that the game's "equipment" with two changes in the dimension-a change in the dimension of "platform changes ", the other Change dimension is" change of model ". If we are going to write code to implement this game, do we implement a separate set of "equipment" for each platform? Where is the reuse? How to deal with this "multidimensional change"? How do you use object-oriented technology to make "gear" easy to move along the "platform" and "model" two directions without introducing additional complexity?

2.2. Intention (Intent)

Separate the abstractions from the implementation, so that they can all change independently.

Bridge mode cannot be considered merely a separation of abstraction and implementation, and it is not limited to this. In fact, two are abstract parts, more precise understanding, it should be a thing in a number of dimensions of the change of separation。

2.3. Structure Diagram



2.4, the composition of the model

The structure of the bridging mode consists of five parts, abstraction, Refinedabstraction, Implementor, Concreteimplementora, and Concreteimplementorb, where:

. Abstraction: Defines an interface for an abstract class that maintains a pointer to an Implementor type object.

. Refinedabstraction: Expands the interface defined by abstraction;

. Implementor: The interface that defines the implementation class, which is not necessarily identical to the interface of the abstraction, in fact two interfaces can be completely different. In general, the Implementor interface provides only basic operations, while abstraction defines a higher level of operation based on basic operations.

. Concreteimplementora and Concreteimplementorb: Implements the Implementor interface and defines its specific implementation.

In bridging mode, two classes abstraction and Implementor define the interface of the abstract and the behavior type respectively, and implement the dynamic combination of abstraction and behavior by invoking subclasses of the two interfaces.

2.5, the concrete realization of bridge connection mode

Today we will use the database as an example to write the implementation of this pattern. Each database has its own version, but each database is implemented differently on different platforms. For example: Microsoft's SQL Server database, which has 2000 version, 2005 version, 2006 version, 2008 version, followed by a newer version. And these versions are all running under the Windows operating system, what if you want to provide SQL Server under the LUnix operating system? What if I want to provide SQL Server database under iOS OS again? In this case, you can use the bridging mode, which is the brige mode. Let's take a look at the concrete implementation!

1 namespaceimplementation of bridging mode2 {3     /// <summary>4     ///the abstract class is the definition of an abstract interface, which is equivalent to the abstraction type5     /// </summary>6      Public Abstract classDatabase7     {8         //The platform interface is referenced by a combination, and here is the bridge, which is equivalent to the implementor type9         Privateplatformimplementor _implementor;Ten  One         //through the constructor injection, initialize the platform implementation A         protectedDatabase (platformimplementor implementor) -         { -             This. __implementor=implementor; the         } -  -         //Create a database-this action is equivalent to the operation method of the abstraction type -          Public Abstract voidCreate (); +     } -  +     /// <summary> A     ///the abstract class is the definition of the implementation interface, which is equivalent to the implementor type at     /// </summary> -      Public Abstract classPlatformimplementor -     { -        //This method is equivalent to the Operationimpl method of the implementor type. -         Public Abstract voidProcess (); -     } in   -     /// <summary> to     ///SqlServer2000 version of the database, equivalent to the refinedabstraction type +     /// </summary> -      Public classSqlserver2000:database the     { *          Public Override voidCreate () $         {Panax Notoginseng              This. _implementor. Process (); -         } the     } +   A     /// <summary> the     ///SqlServer2005 version of the database, equivalent to the refinedabstraction type +     /// </summary> -      Public classSqlserver2005:database $     { $          Public Override voidCreate () -         { -              This. _implementor. Process (); the         } -     }Wuyi  the    /// <summary> -     ///SqlServer2000 version of the database for UNIX operating system specific implementation, equivalent to the Concreteimplementora type Wu     /// </summary> -      Public classSqlserver2000uniximplementor:platformimplementor About     { $          Public Override voidProcess () -         { -             //SqlServer2000 specific implementation of UNIX; -         } A     } +   the     /// <summary> -     ///SqlServer2005 version of the database for UNIX operating system specific implementation, equivalent to the Concreteimplementorb type $     /// </summary> the      Public Sealed classSqlserver2005uniximplementor:platformimplementor the     { the          Public Override voidProcess () the         { -             //SqlServer2005 specific implementation of UNIX; in         } the     } the  About     Public class Program the    { the       Static voidMain () the       { +Platformimplementor sqlserver2000uniximp=Newsqlserver2000uniximplementor (); -          //can also be extended for different platforms, that is, sub-class, this is the independent change the BayiDatabase sqlserver2000unix=NewSqlServer2000 (sqlserver2000uniximp); the          //The database version can also be extended and upgraded, and also independently changed.  the  - The above is the change of two dimensions.  -  the          //you can perform operations on UNIX. the sqlserver2000unix.create (); the       } the    } -}

The

    code is very simple, also has the detailed comment, will not say more.

Three, bridging mode implementation points:
    
    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, that is, "subclass" them, after the various subclasses, they can be arbitrarily combined to obtain different models on the platform.

    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.

iv. Implementation of bridge mode in. NET

    Learning ... , if anyone has good code to share, can also be posted out.

v. Summary

  Today's article is written here, now a summary. Bridging mode It is a bridge between the client code and the implementation code, and it also isolates the effect of changes in the implementation code on the customer code. In the "intent" of the abstraction and implementation, these two parts are actually highly abstract, the front "abstract" refers to the definition of the interface for the client, the client is actually using the abstract type or the Refinedabstract type, these two types are just interfaces, The specific implementation is delegated to the implementor type, and the extension of the abstract type subclass also evolves into the implementor subclass. My personal understanding is that the abstract type and its subtypes act as bridges between the client code and the really implemented code, isolating the changes in the Implementor implementation code and making the client more stable, so the intent is to say that the abstract part and its implementation are partially isolated. Let's have a good understanding of it, just started a bit around.

C # design mode BA Qiao Yangzhong mode (bridge) "structured"

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.