1、 Reasons for appearance

1、 The same type , Yes Two dimensions of change ( The abstraction of two dimensions : The abstraction of an abstract part , An abstraction of the implementation part )

2、 How to deal with this “ Multidimensional change ”? How to use object-oriented technology to make the same type easily change in two directions , Without introducing extra complexity ?

2、 Specific examples

Database operation

Operation type : Additions and deletions   ( The realization of specific functions )

Action object : Customer , Order , product …( Corresponding to the above addition, deletion, modification and query is  1 Relationship to many ( By aggregating relationships ))

Mobile software

software function : Mail list , game

Support the brand :M( Motorcycle mule pull ),N( Here is the chicken )

Computer software

software function : game , development tool , Drawing software …

Operation platform :Windows,Unix….

Overuse inheritance :

—— Take mobile software as an example :

By mobile phone brand :

Classified by mobile phone software :

Let's watch carefully , If you add a mobile phone software to the above ,, Then every mobile phone brand should add this mobile phone software , Everything has to change , Too high coupling 了 .

Use bridge mode to refactor the above :

3、 Intention :

take Abstract part And Achieve partial separation , So that they can all Change independently .

4、 chart :

5、 Code demonstration

 // Bridging mode : Changes in two dimensions ( One is abstraction , One is to realize ), also These two dimensions It's one to many ( abstract 1 For more than Realization )/ polymerization Relationship ; Bridge mode will Two dimensions ( Abstraction and Implementation ) Isolation . take Changes in two dimensions encapsulated , No matter one dimension Yes? change It doesn't affect the other dimension change .. therefore Coupling degree To reduce the quite a lot ( Compared with the previous Inheritance relationships )
// The first dimension ( Implementation part )
public interface IPhoneSoftware
{
void Start();
} // The second dimension ( Abstract part ) public abstract class PhoneBrand
{
private List<IPhoneSoftware> phoneSoftwares = new List<IPhoneSoftware>(); public void Instance(IPhoneSoftware software)
{
phoneSoftwares.Add(software);
} public void StareAllSoftware()
{
if (phoneSoftwares.Count <= )
{
throw new Exception(" There's no executable Mobile Software !");
}
else
{
foreach (var item in phoneSoftwares)
{
item.Start();
}
}
}
} // The first dimension ( Implementation part : Implemented function )
public class KuGou : IPhoneSoftware
{
public void Start()
{
Console.WriteLine(" Cool dog music is on , You can enjoy the music !");
}
} public class QQ : IPhoneSoftware
{
public void Start()
{
Console.WriteLine(" QQ It's on , It's time to chat !");
}
} // The second dimension ( The abstract part of the concrete )
public class Samsung : PhoneBrand
{ } public class Iphone : PhoneBrand
{
}

Birdge Pattern

Client code :

 // Create abstraction 
PhoneBrand samsung = new Samsung();
// Create an implementation
IPhoneSoftware soft1 = new QQ();
IPhoneSoftware soft2 = new KuGou();
// Combine abstraction with implementation
samsung.Instance(soft1);
samsung.Instance(soft2); samsung.StareAllSoftware();

6、 To achieve the point

1、Abstraction( Abstract part ): Define the interface of an abstract class ;; And maintain the direction of Implementor class ( Implementation part ) Of Object pointer .( Changes in the abstract part don't affect the implementation part , The change in the implementation part doesn't affect the abstract part , Because they are through   Aggregate relationship   Connected , Expansion is just for them The respective abstractions   Conduct Expand change )

2、Implementor: Definition Implement the interface of the class , The interface does not have to be with Abstraction The interface of is exactly the same . In fact, these two interfaces can be completely different .( Because it's right   The abstraction of each aspect ( yes Two different dimensions ), adopt   Aggregate relationship   Connect )

3、 generally speaking ,Implementor Interface Available only Basic operation Realize the specific functions of the part ), and Abstraction( It's right   Concrete realization Of   call ) Then we define operation based Higher level operations .

7、 effect

1、 take Interface ( abstract ) Separate from implementation : An interface can have several implementations ( Aggregate relationship ), An implementation can also serve several objects ( The specific functions of an implemented part   Can be done   Reuse ,, You can have multiple objects ( Abstract the following is concrete   Extended abstraction ) Inside for use ), Objects representing logic Sure Dynamically And Realization Functional Object combination .

2、 Improve scalability Logic and Implementation Both can pass Class level extension To expand .

3、 Logic and implementation are encapsulated in different objects , The effect of logic on the realization of quote yes Dynamic advance Yes . In practice, , You need to use different “ factory ” Create logic and implement , Then assemble .

8、 Applicability

1、 Don't want to In the business ( Abstract part ) And business software ( Implementation part ) There is a fixed... Between implementations Binding relationship . for example , Don't want to “ Put in storage ” Business process is too closely related to specific database access technology or database management system .

2、 hope Class abstraction and implementation part Sure expand , Then, it realizes the combination of different abstract interfaces and implementation parts .( The implementation part can be in different abstract parts   Can be reused )

Modify the implementation part For users No impact , namely The code doesn't need to be recompiled .( Because the user The operation is the implementation interface , Concrete The implementation part below the implementation interface already encapsulation up )

3、 Reuse implementation part . Because the implementation part is at a lower level , Therefore, it can be reused by multiple business modules . for example , Database access module can be used in a variety of business units .

9、 summary

1、Bridge Mode use “ Between objects Combine Relationship ” decoupling Abstraction and Implementation Inherent between binding Relationship , So that abstraction and implementation can Change along their own dimensions .

2、 The so-called abstraction and implementation change along their respective dimensions , namely “ Subclass ” they . After getting the subclasses , Can then Any combination of them .

3、Bridge Patterns are sometimes similar to multiple inheritance schemes , But multiple inheritance schemes tend to Against the principle of single responsibility ( namely A class has only one reason to change ), Poor reusability .Bridge Patterns are a better solution than multiple inheritance schemes .

4、Bridge Patterns are used in “ Two Very strong dimension of change ”( There are two directions that can be derived ), Sometimes even if there are two dimensions of change , But the dimension of change in one direction is not dramatic —— In other words, two changes It doesn't lead to crisscross Result , and You don't have to use Bridge Pattern Design pattern is an idea , It doesn't have to be hard ).

Design pattern Birdge( The bridge ) More articles on patterns

  1. Java Design pattern 《 Bridging mode 》 And application scenarios

    Abstract : Original works , Can be reprinted , But please mark the source address http://www.cnblogs.com/V1haoge/p/6497919.html Here is an excerpt of a concept from other places , You can ignore , Let's look at the following explanations and examples , ...

  2. Instance analysis Python The application of bridge mode in design pattern programming

    Instance analysis Python The application of bridge mode in design pattern programming This article mainly introduces Python The application of bridge mode in design pattern programming , The bridging pattern advocates separating the abstract from its implementation , Friends in need can refer to Let's start with an example : #e ...

  3. java23 Design patterns —— 7、 ... and 、 Bridging mode

    Original address :https://www.cnblogs.com/chenssy/p/3317866.html The source code is in mine github and gitee In order to get Catalog java23 Design patterns -- One . Design pattern introduction j ...

  4. Net Bridge mode of design pattern instance ( Bridge Pattern)

    One . Introduction to bridge mode (Brief Introduction) Bridging mode (Bridge Pattern), Separate the abstract from its implementation , So that the abstraction and implementation of can be changed independently . Decouple an abstra ...

  5. [ Design patterns ] javascript And Bridging mode

    Bridge mode description Definition : Separation of abstraction and realization , Make it free to change independently : explain : Due to the requirement of software environment , Make the type abstract have many kinds of implementation, define with its own change and so on , This allows us to separate the abstract implementation from the concrete implementation , Decouple abstraction from implementation , ...

  6. Bridging mode of design pattern learning (Bridge, Structural mode )(15)

    Reference address :http://terrylee.cnblogs.com/archive/2006/02/24/336652.html summary In software system , Some types have their own logic , It has two or more dimensions of change , ...

  7. 【 Reading notes 】 read 《JavaScript Design patterns 》 Bridge mode of

    Bridging mode (Bridge) Separate the abstract from its implementation , So that they can all change independently . One . Use scenarios Use scenario 1 : Event monitoring For the front end , The most typical use scenarios —— Event monitoring . Such as —— addEvent(element ...

  8. Java Bridge mode of design pattern series

    Bridging mode (Bridge) The definition of In software system , Some types have their own logic , It has two or more dimensions of change , So how to deal with this “ Multidimensional change ”? This uses bridging mode Separate the abstract from its implementation , Make them all independent ...

  9. java Design patterns -----14、 Bridging mode

    Bridge Patterns are also called bridging patterns , Is one of the design patterns of stereotypes .Bridge Patterns are based on the minimum design principles of classes , By using encapsulation , Aggregation and inheritance behavior to make different classes take on different responsibilities . Its main feature is to abstract (abstraction ...

  10. Design pattern learning experience &lt; Bridging mode Bridge&gt;

    Seriously, before that , There is almost no application scenario concept for bridging mode . The bridge (Bridge) It is used to decouple abstraction and realization , So that they can change independently . This type of design pattern belongs to structural pattern , It provides a bridge between abstraction and Implementation , Come on ...

Random recommendation

  1. JS What does scripting language mean ?

    javascript,Javascript It's a browser side scripting language , It is used to handle the interaction with users in the web client , And the realization of page effects . For example, verify the validity of the data before submitting the form , Reduce server errors and stress . According to the customer operation , Give some improvement ...

  2. X509 Certificate generation

    X509 Certificate Introduction X.509 By the International Telecommunication Union (ITU-T) Digital certificate standards , I believe this is well known , at present X.509 There are three versions of the certificate as far as I know ,.net Is used in x.509-2,X.509-2 Version introduces the main body ...

  3. C++ Basics ( Pure virtual functions and abstract classes )

    C++ Basic pure virtual functions and abstract classes introduction Pure virtual functions in C++ The place in programming is very important , It's related to design patterns " Interface " The concept of . grammar The syntax of pure virtual functions : 1.  Declare the member function as virtual 2.  ...

  4. Linux Shell Batch change file name or suffix

    Put all of the following .c Change the file name of to .cc rename .c .cc *.c

  5. Chap5: question: 29 - 31

    29. A number that appears more than half the times in an array . Method a. Sort the middle        O(nlogn). Method b. partition Function split to find the median      >=O(n). Method c. Design variables , Scan it again ...

  6. css“ deformation ” effect

    <html <head> <title></title> <style> .test { margin-left:300px; margin-to ...

  7. HW2.24

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  8. USACO3.42American Heritage( Binary tree )

    Known before Ask for the next order Recursion There are some small details /* ID: shangca2 LANG: C++ TASK: heritage */ #include <iostream> #include& ...

  9. From the initialization of linked list C A secondary pointer to a language

    First look C Language to create linked lists . Insert the node and traverse the list of a piece of code : #include <stdio.h> #include <stdlib.h> typedef int ElemType; ...

  10. Zhejiang University PAT The test 1009~1012(1010 God is the champion ..)

    Ah ,pat1010 Even if java A written , Only java A written , Two more points have been added ,,. ah , Good IQ , The main pat Some don't give clear scope . Create a reverie space .. Or in order .. Title address :http://pat.zju.ed ...