Design Pattern bridging

Source: Internet
Author: User

Design Pattern bridging
Bridge Pattern)
It is also called the bridge mode. Separate the abstract part from its implementation part so that they can all change independently.


In home life, everyone has used electric lights. switches and electric lights are connected through wires to control the lights. Nowadays, various materials and products are everywhere, and there are also a variety of switches and electric lights. For example, the types of switches include click switches, voice-controlled switches, touch switches, and rotary switches. There are even more types of electric lights, such as incandescent lamps, neon lights, and crystal lamps.

If you want to change a crystal lamp at home with so many switches and lights, do you need to leave the wall behind and re-design the line to install the crystal lamp? Of course not. Switch connected power supply, wire transmission power supply, light is only responsible for lighting! Perform their respective duties and do not interfere with each other. Therefore, if you want to change the crystal lamp, you do not have to change the power cord of the switch box, as long as you change the light set. The same is true for switch replacement.


Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace bridging {// lamp interface public interface ILight {// power on void electricConnected (); // lighting void light (); // power off void electricClosed ();} // switch the top-level class public class BaseSwitch {// use a combination and set ILight to an internal private attribute, which is the bridge protected ILight light; // constructor injects external light types into public BaseSwitch (ILight light) {this. light = lig Ht;} // open the light method public void makeLight () {// turn on the switch and power on this. light. electricConnected (); // lighting this. light. light (); // turn off the switch and disconnect the power supply this. light. electricClosed () ;}}// remote control switch public class RemoteControlSwitch: BaseSwitch {// constructor public RemoteControlSwitch (ILight light): base (light) {} // use the remote control switch to open the public void makeRemoteLight (int operColor) {// turn on the switch and power on this. light. electricConnected (); // lighting this. light. light (); String color = ""; switch (operColor) {case 1: color = "warm"; break; case 2: color = "blue"; break; case 3: color = "red"; break; case 4: color = "white"; break;} Console. writeLine ("... now it is "+ color + "! "); // Turn off the switch and disconnect the power supply this. light. electricClosed () ;}}// incandescent public class IncandescentLight: ILight {// power on public void electricConnected () {Console. writeLine ("Incandescent is turned on... ");} // disconnect the public void electricClosed () {Console. writeLine ("Incandescent is disabled... ");} // lighting public void light () {Console. writeLine ("incandescent lighting! ") ;}}// Crystal light implements public class CrystalLight: ILight {// power on public void electricConnected () {Console. writeLine ("the crystal light is turned on... ");} // lighting public void light () {Console. writeLine ("crystal light! ");} // Disconnect the public void electricClosed () {Console. writeLine ("the crystal light is off... ") ;}} class Program {static void Main (string [] args) {// incandescent instance ILight incandescentLight = new IncandescentLight (); // crystal light instance ILight crystalLight = new CrystalLight (); Console. writeLine ("-- General switch --"); // General switch BaseSwitch swititch = new BaseSwitch (incandescentLight); switch1.makeLight (); Console. writeLine ("\ n -- remote control switch --"); // remote control switch RemoteControlSwitch switch2 = new RemoteControlSwitch (crystalLight); switch2.makeRemoteLight (1 );}}}




As mentioned above, what should we do if we want to use a general switch and remote control crystal light? At this time, we only need to pass the crystal lamp into the construction method of the general switch to achieve! <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/qrnYsr + 31qO6PGJyPgo8cHJlIGNsYXNzPQ = "brush: java;"> // General switch BaseSwitch swititch = new BaseSwitch (incandescentLight); switch1.makeLight ();To:
// General switch BaseSwitch swititch = new BaseSwitch (crystalLight); switch1.makeLight ();



Class diagram of the Bridge Mode:
The scalability of the system. You do not need to modify the original system to expand any one of the two dimensions, which complies with the "Open and Close principles ".

Usage:
1. You do not want to have a fixed binding relationship between the abstract class and its implementation.
2. Class abstraction and implementation should be expanded by generating sub-classes
3. Modifications to an abstract implementation part should not affect the customer, that is, the customer's Code does not have to be re-compiled.

Differences from adapters:
An adapter is the design mode for converting an interface to another interface, while a bridge separates the implementation from its interface so that they can change independently. Bridging is not used to connect an existing object to an interface that does not match.






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.