a schema definition
Bridge mode, also known as bridge mode, in the software system, due to its own logic, has two or more dimensions of change, how to deal with this multi-dimensional change, the bridge mode enables the software system to easily follow multiple directions Make changes without introducing additional complexity.
The three keywords of the bridge pattern are: abstraction, realization, decoupling
Two-mode example
1 Bridge mode analysis method
We use electric lighting to illustrate this pattern.
Instead of using inheritance, use the method of object composition to change the strong relationship between the switch and the light into a weak relationship.
2 Bridge mode static class model
3 Code Examples
3.1 Create the light interface
package com.demo.bridge.lights; /** * Lighting interface * * @author * */ public interface ILight { // turn on current public void electricConnected(); // lighting public void light(); // current off public void electricClosed(); }
3.2 Creating general switches
package com.demo.bridge.switchs; import com.demo.bridge.lights.ILight; /** * Switch top-level class * * @author * */ public class BaseSwitch { // Use composition to set ILight as an internal private property this is a bridge protected ILight light; // The constructor injects the external light type public BaseSwitch(ILight light) { this.light = light; } /** * How to turn on the light */ public final void makeLight() { // turn on the switch to turn on the current this.light.electricConnected(); // lighting this.light.light(); // close the switch to turn off the current this.light.electricClosed(); } }
3.3 Create a remote switch
package com.demo.bridge.switchs.sub; import com.demo.bridge.lights.ILight; import com.demo.bridge.switchs.BaseSwitch; /** * The remote control switch inherits the extended function of BaseSwitch * * @author * */ public class RemoteControlSwitch extends BaseSwitch { // Construction method public RemoteControlSwitch(ILight light) { super(light); } /** * Use the remote control switch to control the lights * * @param operColor * light color */ public final void makeRemoteLight(int operColor) { // turn on the switch to turn on the current this.light.electricConnected(); // lighting this.light.light(); String color = ""; switch (operColor) { case 1: color = "暖色"; break; case 2: color = "blue"; break; case 3: color = "red"; break; default: color = "白色"; break; } System.out.println(" ...现在是" + color + "!"); // close the switch to turn off the current this.light.electricClosed(); } }
3.4 Incandescent Lamp Implementation
package com.demo.bridge.lights.impl; import com.demo.bridge.lights.ILight; /** * Incandescent lamp implementation * * @author * */ public class IncandescentLight implements ILight { // current off public void electricClosed() { System.out.println("The incandescent lamp is turned off..."); } // turn on current public void electricConnected() { System.out.println("The incandescent lamp is turned on..."); } // lighting public void light() { System.out.println("Incandescent lighting!"); } }
3.5 Crystal Lamp Realization
package com.demo.bridge.lights.impl; import com.demo.bridge.lights.ILight; /** * Crystal light realization * * @author * */ public class CrystalLight implements ILight { // current off public void electricClosed() { System.out.println("The crystal lamp is turned off..."); } // turn on current public void electricConnected() { System.out.println("The crystal lamp is turned on..."); } // lighting public void light() { System.out.println("Crystal Lighting!"); } }
3.6 The general switch controls the incandescent lamp, and the remote control switch controls the crystal lamp
package com.demo; import com.demo.bridge.lights.ILight; import com.demo.bridge.lights.impl.CrystalLight; import com.demo.bridge.lights.impl.IncandescentLight; import com.demo.bridge.switchs.BaseSwitch; import com.demo.bridge.switchs.sub.RemoteControlSwitch; /** * Client application * * @author * */ public class ClientForBridge { /** * @param args */ public static void main(String[] args) { // Incandescent lamp instance ILight incandescentLight = new IncandescentLight(); // crystal lamp instance ILight crystalLight = new CrystalLight(); // general switch System.out.println("-- General switch-- "); BaseSwitch switch1 = new BaseSwitch(incandescentLight); switch1.makeLight(); System.out.println("\n-- Remote control switch-- "); // remote control RemoteControlSwitch remoteControlSwitch = new RemoteControlSwitch( crystalLight); remoteControlSwitch.makeRemoteLight(1); } }
operation result:
-- General switch--
The incandescent light was turned on...
Incandescent lighting!
Incandescent lights are turned off...
-- remote control--
The chandelier is turned on...
Crystal Lighting!
...now in warm colors!
The chandelier is turned off...
3.7 The general switch controls the crystal lamp, the remote control switch controls the incandescent lamp
package com.demo; import com.demo.bridge.lights.ILight; import com.demo.bridge.lights.impl.CrystalLight; import com.demo.bridge.lights.impl.IncandescentLight; import com.demo.bridge.switchs.BaseSwitch; import com.demo.bridge.switchs.sub.RemoteControlSwitch; /** * Client application * * @author * */ public class ClientForBridge { /** * @param args */ public static void main(String[] args) { // Incandescent lamp instance ILight incandescentLight = new IncandescentLight(); // crystal lamp instance ILight crystalLight = new CrystalLight(); // general switch System.out.println("-- General switch-- "); BaseSwitch switch1 = new BaseSwitch(crystalLight); switch1.makeLight(); System.out.println("\n-- Remote control switch-- "); // remote control RemoteControlSwitch remoteControlSwitch = new RemoteControlSwitch( incandescentLight); remoteControlSwitch.makeRemoteLight(1); } }
operation result
-- General switch--
The chandelier is turned on...
Crystal Lighting!
The chandelier is turned off...
-- remote control--
The incandescent light was turned on...
Incandescent lighting!
...now in warm colors!
Incandescent lights are turned off...
Three design principles
1 Try to use objects to aggregate weak associations and avoid using inherited strong associations.
2 Decoupling of abstraction and realization.
Four use cases
1 do not want to have a fixed binding relationship between the abstract class and the implementation part
Both the abstraction and the implementation part of the 2 classes should be extended by the methods of the lone class
3 Modifications to an abstract implementation part have no effect on the client, that is, the client code does not have to be recompiled
Five bridge mode static class diagram