. NET Bridging mode explained

Source: Internet
Author: User
Definition of bridging Mode:

Decoupling abstractions (abstraction) from implementation (implementation) so that they can vary independently.

Bridge mode structure diagram:

Role in bridging mode:

Abstract (abstraction) Role: Abstract The given definition and save a reference to the implemented object.
Fix abstract (refined abstraction) roles: Extend abstract roles, change and modify the definition of the parent class for abstraction.
Implementation-Implementor role: This role gives the interface to the implementation of the role, but does not give a specific implementation. It must be noted that this interface is not necessarily the same as the interface definition of the abstract role, in fact, the two interfaces can be very different. The implementation role should only give the underlying operation, and the abstract role should give only a higher level of operation based on the underlying operation.

Combined with example Description:

Reference a TV Remote control example, for each brand of the remote control, there are echoes of the remote controls, at this time we think of the set may be: Abstract a remote control interface, which is to be implemented in the boot, shutdown, switch channels such a set of functional methods. Then create the specific remote control class to inherit this interface, implement the method inside. This allows each TV to implement its own remote control, for the addition of other types of TV, only need to add a derived class to meet the new remote control derivation. But one day, the user asked to add a function to return to the previous channel in the remote control, you need to change the abstraction of the remote control interface, you need to add a new method to the abstract class, which changes the implementation of the abstract class. If the user requests to change the product behavior of the TV at the same time, and the remote Control behavior method, for the above design will cause great changes. The use of bridging mode can be a good solution to these problems.

Use:

1. First abstract out the TV, provide the remote control to change the behavior of the method.

<summary>///TV, providing abstract methods///</summary>public abstract class tv{public abstract void on (); public abstract void Off (); public abstract void Tunechannel ();}

2. Create a specific TV set, inherited from the abstract TV class:

<SUMMARY>///Samsung TV, overriding the abstract method of the base class///</summary>public class samsung:tv{public override void on () {  Console.WriteLine ("Samsung TV has been turned on"); } public  override void Off () {  Console.WriteLine ("Samsung TV has been turned off");}  public override void Tunechannel () {  Console.WriteLine ("Samsung TV Change Channel")}///<summary>///Changhong TV, overriding base class abstract method// Provide specific implementations///</summary>public class changhong:tv{public override void on () {  Console.WriteLine ("Changhong TV has been turned on") ; } public  override void Off () {  Console.WriteLine ("Changhong TV has been turned off");  public override void Tunechannel () {  Console.WriteLine ("Changhong TV Change Channel");}}

3. Then abstract the remote control in the overview, playing the role of abstract words.

<summary>///abstract concept of remote control, playing abstract role///</summary>public abstract class remotecontrol{public TV implementor { Get Set }///<summary>///TV////The implementation  is no longer provided in the abstract class, but instead is implemented in the implementing class.//</summary> public virtual void on () {  imple Mentor. On (); }  ///<summary>///TV////</summary> public virtual void off () {  implementor. Off (); }///<summary>//  Change channel///</summary> public virtual void Setchannel () {  implementor.tunechannel (); }}

4. Create a specific remote control class: Here, I rewrite the method of changing channels, in fact, can also rewrite other methods.

<summary>///specific remote control class///</summary>public class concreteremote:remotecontrol{//<summary>// Override Change Channel method///</summary> public override void Setchannel () {  Console.WriteLine ("Override Channel Change Method");  Base. Setchannel (); }}

5. Call:

static void Main (string[] args)  {   //Create a remote control   remotecontrol Remotecontrol = new Concreteremote ();       Changhong TV   remotecontrol.implementor = new Changhong ();   Remotecontrol.on ();   Remotecontrol.setchannel ();   Remotecontrol.off ();   Console.WriteLine ();    Samsung Brand TV   Remotecontrol.implementor = new Samsung ();   Remotecontrol.on ();   Remotecontrol.setchannel ();   Remotecontrol.off ();   Console.read ();  }

In this way, the realization of the bridge mode design, the function of the remote control is not implemented in the remote control, but will be implemented to another TV class to encapsulate it, the remote control only contains a reference to the TV class, through bridging mode, we have abstracted and realized part of the separation, This is a good way to deal with these two changes.

Advantages:

Abstract interfaces are decoupled from their implementations, and the abstractions and implementations of the interim can be independently extended without affecting each other.

Disadvantages:

Increases the complexity of the system.

Usage scenarios:

1. If a system needs to add more flexibility between the component's abstract role and the materialized role, avoid establishing a static connection between the two levels
2. Any change in design that requires the implementation of a role should not affect the client, or the change in the implementation of the role is completely transparent to the client.
3. Graphics and windowing systems that need to span multiple platforms
4, a class has two independently changing dimensions, and two dimensions need to be expanded.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

  • Related Article

    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.