Design Mode learning notes-Bridging Mode

Source: Internet
Author: User

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.

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.