Overview:
Separate the abstract part from its implementation part so that they can all change independently.
Usage:
1. You do not want to have a fixed binding relationship between the abstraction and its implementation. For example, this may be becauseProgramSome applications can be selected or switched during runtime.
2. Class abstraction and its implementation can be expanded by generating sub-classes.
3. Modifications to an abstract implementation part should not affect the customer, that is, the client'sCodeYou do not need to re-compile.
4. You want to share the implementation among multiple objects, but at the same time, the customer is not required to know this.
Class diagram:
Sample Code:
1. abstract interface class
Abstract ClassImplementor
{
Public Abstract VoidOperation ();
}
2. Implement the derived classes A and B of the abstract interface
Class Concreteimplementora: implementor
{
Public Override Void Operation ()
{
Console. writeline ( " Implementation of method " );
}
}
Class Concreteimplementorb: implementor
{
Public Override Void Operation ()
{
Console. writeline ( " Implementation of Method B " );
}
}
3. Abstract aggregation class of specific implementation methods, but the actual implementation is in its derived class, that is, as an important part of intermediate bridging, expressing the core idea of the Bridge Mode
Abstract Class Invalid action
{
Protected Implementor;
Public Void Setimplementor (implementor)
{
This . Implementor = Implementor;
}
Public Virtual Void Operation ()
{
Implementor. Operation ();
}
}
4. inherit the aggregation class and ultimately implement the virtual method to be bridging, and expand the interface defined by the aggregate action
ClassRefinedabstraction: Invalid action
{
Public Override VoidOperation ()
{
Implementor. Operation ();
}
}
5. Client call
/// <Summary>
/// Test the Bridge Mode
/// </Summary>
Static Void Testbridge ()
{
Billing action AB = New Refinedaskaction ();
AB. setimplementor ( New Concreteimplementora ());
AB. Operation ();
AB. setimplementor ( New Concreteimplementorb ());
AB. Operation ();
Console. Read ();
}
Summary:
The Bridge Mode also encapsulates a change of structure pattern abstraction. Its core idea is to rely on abstraction rather than specific implementation. However, in many cases, it cannot be copied directly, most of the complex cases combine various design patterns. They are not completely routines. Just like practicing martial arts, beginners need to master routines and ideas. At present, I am learning dual-stick, programmers who share common interests can learn from each other.