User-friendly design Model – Bridge Mode


Today explain that the structural design pattern in the second – bridging mode. Bridging – refers to the link layer address based on the OSI network model, the network data packet forwarding process, the operation of the second layer of OSI. General switches, bridges, there are bridging role. This is the Wikipedia explanation. So what about today’s bridge mode is it? Bridge is a link, in recent years, the Hong Kong-Zhuhai-Macao Bridge. He served as a link connecting the three. So in design mode bridging model? Is the same, wherein the composition is through a bridge, connecting link. Then combined into what is needed. Specifically, we look at the specific meaning of its bridge mode now.

Bridge Mode Introduction

First, the reason

When we are faced with systems development and design, sometimes faced with some classes there will be changes in two dimensions, in a variety of circumstances may change, inheritance causes more complex procedures consequence, it is not extended so flexible. So how to deal with the situation classify multiple angles and multiple angles are likely to change it? This requires a bridging mode.

Second, the intent

Abstract achieve partial separation portion, so that they can be independently varied.

Third, the case of FIG.




Fourth, the bridge mode code sample

See above diagram shows the case, we can see the bridge mode includes the following components:

First, the role of abstraction: abstraction give the definition, contains a reference to the realization of the object

Second, the specific role of abstraction: abstraction extension implementation role

Third, the realization of the role: This role gives the realization of the role of the interface, but no implementation. This role does not necessarily require the same role with abstraction. Conversely it can be completely different. Abstraction corresponds to a dimension corresponding to the role, and the realization of the character corresponding to the second dimension. Fourth, the specific implementation of the role: Extended to achieve the realization of the role

We see such a case. Well known phone has Android and IOS type. Then the software is also required for the adaptation of both types. At the same time it is in constant iterative software update. I was so concerned. For a mobile phone software. Let us look at two dimensions. A, iteration later. Second, suitable for the type of phone. If inheritance way to solve this type of problem would greatly increase the complexity of the system. Here we can use the bridge mode. We take a look at how the code is designed to achieve it:


namespace Bridge_Pattern
    class BridgePattern
    #region  抽象化角色——迭代更新的版本(包含对实现化角色对象的引用)========
    public abstract class Version
        /// 实现对实现化对象的引用,通过组合实现软件迭代更新并适用于不同平台的功能
        protected PhoneType _phoneType;

        /// 构造函数注入,实现化对象的初始化
        public Version(PhoneType phoneType) 
            this._phoneType = phoneType;
        /// 创建软件版本
        public abstract void Create();

    #region 实现化角色——适用于手机类型=============================================
    public abstract class PhoneType 
        /// 适配手机类型
        public abstract void SetType();

    #region 具体抽象化角色——具体实际迭代更新的版本===========
    /// 版本1.0
    public class OneVersion : Version
        public OneVersion(PhoneType phoneType) :base(phoneType) 
        public override void Create()

The current version 1.0

"); this._phoneType.SetType(); } } /// /// 当前版本2.1 /// public class TwoVersion : Version { public TwoVersion(PhoneType phoneType) : base(phoneType) { } public override void Create() { Console.WriteLine("

The current version 2.0

"); this._phoneType.SetType(); } } #endregion #region 具体实现化角色——具体实际使用类型=================== /// /// 安卓系统 /// public class AndroidPhoneType : PhoneType { public override void SetType() { Console.WriteLine("

Android is the current type

"); } } /// /// ios系统 /// public class IOSPhoneType : PhoneType { public override void SetType() { Console.WriteLine("

IOS is the current type

"); } } #endregion }


    class Program
        static void Main(string[] args)

Android 1.0 version of the software system

AndroidPhoneType androidPhoneType = new AndroidPhoneType(); OneVersion oneVersion = new OneVersion(androidPhoneType); oneVersion.Create(); ///

IOS system software version 2.0

IOSPhoneType iOSPhoneType = new IOSPhoneType(); TwoVersion twoVersion = new TwoVersion(iOSPhoneType); twoVersion.Create(); Console.ReadLine(); } }


Usage scenarios and the advantages and disadvantages

First, the use scene

1, if the system needs to be built to add some flexibility between abstraction and implementation role of roles, avoiding the form of class inheritance. Relationship can be established using bridge mode

2, if the system does not wish to adopt the form of inheritance or succession will cause the system to form extremely complex, consider using bridge mode

3, a class if there are two dimensions, and dimensions are often two expansion changes, but used for the bridge mode and then

Second, the advantages

1, the decoupling and separation of the abstract interface

2, abstract and separated with each other, easy to expand

3, details of the implementation transparent to the user

Third, shortcomings

1, increasing the difficulty of understanding the system and the difficulty of design

to sum up

Bridge mode. Meaning to its name. A class is to separate the two dimensions. The abstract interface with decoupling. Then the bridge by combining the associated together. Combination. Which two objects are abstract and subclasses practice. Abstract interface objects referenced by the realization of the role to associate. Bridging mode suitable for two classes of frequently changing dimensions. The separation of multi-angle, then leave them alone to change, reducing the coupling.

So what days and then high, even on tiptoe close to the sun.


C # Design Patterns series directory

Welcome to scan the next Fanger Wei code, and I set foot on the road to pass through the design patterns it!


Leave a Reply