. NET Bridge Mode and. net Bridge

Source: Internet
Author: User

. NET Bridge Mode and. net Bridge

Definition of the Bridge Mode:

Decoupling abstract action and Implementation so that the two can change independently.

Bridge Mode Structure:


Role in Bridge Mode:

Abstract action role: defines the abstract object and saves a reference to the implemented object.
Modify abstract roles: Expand abstract roles, and modify the abstract definitions of the parent class.
Implementor role: this role provides an interface for implementing the role, but does not provide a specific implementation. It must be noted that this interface is not necessarily the same as the interface definition of abstract roles. In fact, these two interfaces can be very different. An implemented role should only provide underlying operations, while an abstract role should only provide operations at a higher level based on underlying operations.

Combined with instance description:

The example of a TV remote control is cited. For each brand of remote control, there is an echo of the remote control to control. At this time, we may think of the following: abstract a remote control interface, it is a set of functions to be implemented, such as starting, shutting down, and changing channels. Then create a specific remote control class to inherit this interface and implement the methods in it. In this way, each TV set can implement its own remote control. If other types of TVs are added, you only need to add a derived class to implement the derivation of the new remote control. However, when you want to add a function to the remote control to return the previous channel, you need to change the abstract Remote Control Interface and add a new method to the abstract class, this changes the implementation of the abstract class. If the user asks to change the product behavior of the TV set and the behavior method of the remote control, the above design will be greatly changed. Using the bridge mode can solve these problems.

Usage:

1. First, extract the TV and provide the behavior of remote control changes.

/// <Summary> // TV set, providing the abstract method // </summary> public abstract class TV {public abstract void On (); public abstract void Off (); public abstract void tuneChannel ();}

2. Create a specific TV set and inherit from the abstract TV set class:

/// <Summary> // TV set with the license plate, override the abstract method of the base class /// </summary> public class Samsung: TV {public override void On () {Console. writeLine ("the licensed TV has been turned on");} public override void Off () {Console. writeLine ("the licensed TV has been turned off");} public override void tuneChannel () {Console. writeLine ("changing channels for brand TV sets"); }}/// <summary> // Changhong TV set, override the abstract method of the base class // provide the specific implementation /// </summary> public class ChangHong: TV {public override void On () {Console. writeLine ("Changhong TV already on");} public override void Off () {Console. writeLine ("Changhong TV has been turned off");} public override void tuneChannel () {Console. writeLine ("Changhong TV channel change ");}}

3. Then the remote control in the overview is abstracted to play the role of abstraction.

/// <Summary> // remote control in the abstract concept, playing the abstract role // </summary> public abstract class RemoteControl {public TV implementor {get; set ;} /// <summary> /// enable the TV // The abstract class is no longer provided here, instead, call the implementation in the implementation class /// </summary> public virtual void On () {implementor. on () ;}/// <summary> // turn Off the 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 have rewritten the method of changing the channel. In fact, I can also rewrite other methods.

/// <Summary> /// specific remote control class /// </summary> public class ConcreteRemote: remoteControl {/// <summary> /// rewrite the method for changing the channel /// </summary> public override void SetChannel () {Console. writeLine ("override the channel replacement method"); base. setChannel ();}}

5. call:

Static void Main (string [] args) {// create a remote control RemoteControl remoteControl = new ConcreteRemote (); // remoteControl for Changhong TV. implementor = new ChangHong (); remoteControl. on (); remoteControl. setChannel (); remoteControl. off (); Console. writeLine (); // picture board TV remoteControl. implementor = new Samsung (); remoteControl. on (); remoteControl. setChannel (); remoteControl. off (); Console. read ();}

In this way, the bridge mode is designed. The function implementation method of the remote control is not implemented in the remote control, but the implementation part is used to encapsulate it in another TV category, the remote control only contains a reference of the TV category. Through the bridge mode, we split the abstract and actual parts away, so that we can well cope with these two changes.

Advantages:

Abstract interfaces are decoupled from their implementations. The abstraction and implementation in the period can be expanded independently without affecting each other.

Disadvantages:

This increases the complexity of the system.

Use Cases:

1. If a system needs to add more flexibility between the abstract role and the specific role of the component, avoid establishing static connections between the two layers.
2. The design requires that any change to the role should not affect the client, or the change to the role should be completely transparent to the client.
3. Graphics and window systems that need to span multiple platforms
4. A class has two independent dimensions, and both dimensions need to be extended.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.