Concept
Separate abstract parts and their corresponding implementation parts so that they can be changed independently.
Role
Implementor -- defines the interface of the implementation class
Createimplementorfirst (createimplementorsecond) -- implements the implementor interface and defines its specific implementation.
Define action -- define abstract interfaces. Is a high-level operation based on implementor.
Refinedmediaaction -- expand another action.
Program Implementation Model
Let's take a look at the implementation of the Program:
Implementor
public abstract class Implementor { public abstract void Operation(); }
Implement implementor
Public class createimplementorfirst: implementor {public override void operation () {console. writeline ("implementation method (1 ). ") ;}} Public class createimplementorsecond: implementor {public override void operation () {console. writeline (" implementation method (2 ). ");}}
Invalid action
public class Abstraction { protected Implementor implementor; public void SetImplementor(Implementor paramImplementor) { this.implementor = paramImplementor; } public virtual void Operation() { implementor.Operation(); } }
Refinedmediaaction
public class RefinedAbstraction : Abstraction { public override void Operation() { implementor.Operation(); } }
Client call:
static void Main(string[] args) { Abstraction abstraction = new RefinedAbstraction(); abstraction.SetImplementor(new CreateImplementorFirst()); abstraction.Operation(); abstraction.SetImplementor(new CreateImplementorSecond()); abstraction.Operation(); Console.ReadLine(); }
Result:
At first glance, there are some meanings of combining the factory model with the policy model. The action is a bit like the factory, createimplementorfirst and createimplementorsecond to implement different policies.
The actual difference is that in the bridging mode, inheritance is generally used for processing when an attribute may have many implementations, and an abstract class is used to define the interface of this attribute; and then implement it in different ways of specific classes. This is equivalent to the combination of the factory mode and the policy mode, but this method is sometimes inconvenient, because inheritance is not easy to maintain, expand, and use attributes and implement independently. The Bridge Mode places abstract attributes and implementations in different class-level architectures to solve this problem. The hierarchical architecture is implemented for the role action and refinedabstraction classes and implementor classes, while the implementation of the createimplementorfirst and createimplementorsecond subclasses of implementor classes is as follows.
Advantages of the Bridge Mode:
1. reducing the coupling between attributes and implementations: the implementation of an attribute is not bound to an interface. Therefore, the implementation of an attribute can be configured during the execution period (configure ); even reducing the coupling between attributes and implementations of objects can also reduce the connection between implementations during compilation. Changing the implementation class requires no re-Compilation of the lateral action class, that is, the user end. This is the most basic feature. When you want to ensure that different versions of the Library can change its implementation during the execution period. Bit compatibility (binary compatibility ). What's more, low coupling can have a better structural system. The higher-order part of the system only needs to know the role action and implementor.
2. improved scalability: You can independently expand the hierarchical architecture of role action and implementor.
3. Hide the detailed implementation on the user side: You can hide the detailed implementation, such as the shared implementor object and the accompanying reference counting mechanism (if any ).
References:
1. Sina Blog