This article describes the use of bridge mode to implement switch and light lighting functions. Share it for your reference, as follows:
1. Pattern definition
Bridge mode, also known as bridge mode, in software systems, due to its own logic, there are two or more dimensions of changes. How to deal with such multi-dimensional changes, bridge mode allows the software system to easily change in multiple directions without introducing additional complexity.
The three keywords of the bridge mode are: abstraction, implementation, and decoupling.
2. Examples of the model
1 Bridge mode analysis method
We borrow light lighting to illustrate this mode.
Without inheritance, use object combination method to turn the strong relationship between switches and lights into weak relationships.
2 Bridge mode static class model
3 Code Examples
3.1 Create an electric light interface
package com.demo.bridge.lights;/** * Light interface* * @author * */public interface ILight{ // Turn on the current public void electricConnected(); // Lighting public void light(); // Current off public void electricClosed();}3.2 Create general switches
package com.demo.bridge.switchs;import com.demo.bridge.lights.ILight;/** * Switch top-level class* * @author * */public class BaseSwitch { // Use combination to set ILight to internal private property This is a bridge protected ILight light; // Constructing method injects external light type into public BaseSwitch(ILight light) { this.light = light; } /** * Light on method*/ public final void makeLight() { // Turn on the switch to turn on the current this.light.electricConnected(); // Lighting this.light.light(); // Turn off 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 BaseSwitch extension function* * @author * */public class RemoteControlSwitch extends BaseSwitch{ // Construct method public RemoteControlSwitch(ILight light) { super(light); } /** * Use the remote control switch to control the light on* * @param operatorColor * Light color*/ public final void makeRemoteLight(int operaColor) { // Turn on the switch to turn on the current this.light.electricConnected(); // Light this.light.light(); String color = ""; switch (operColor) { case 1: color = "warm color"; break; case 2: color = "blue"; break; case 3: color = "red"; break; default: color = "white"; break; } System.out.println(" ...now is " + color + "!"); // Close switch to turn off 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("Incandescent lamp is turned off..."); } // Turn on current public void electricConnected() { System.out.println("Incandescent lamp is turned on..."); } // Lighting public void light() { System.out.println("Incandescent Lighting!"); }}3.5 Crystal lamp implementation
package com.demo.bridge.lights.impl;import com.demo.bridge.lights.ILight;/** * Crystallight implementations * @author * */public class CrystalLight implements ILight{ // Current off public void electricClosed() { System.out.println("Crystallight was turned off..."); } // Turn on current public void electricConnected() { System.out.println("Crystallight was turned on..."); } // Lighting public void light() { System.out.println("Crystal Lamp Lighting!"); }}3.6 General switch control incandescent lamp, remote switch control 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) { // IncandescentLight 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 Switch RemoteControlSwitch = new RemoteControlSwitch( crystalLight); remoteControlSwitch.makeRemoteLight(1); }}Running results:
-- General switch-
The incandescent lamp was turned on...
Incandescent lamp lighting!
The incandescent lamp was turned off...
-- Remote control switch-
The crystal lamp was turned on...
Crystal lamp lighting!
...It's warm now!
The crystal lamp was turned off...
3.7 General switch control crystal lamp, remote control switch control 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) { // IncandescentLight example ILight incandescentLight = new IncandescentLight(); // Crystal lamp example 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 Switch RemoteControlSwitch = new RemoteControlSwitch( incandescentLight); remoteControlSwitch.makeRemoteLight(1); }}Running results
-- General switch-
The crystal lamp was turned on...
Crystal lamp lighting!
The crystal lamp was turned off...
-- Remote control switch-
The incandescent lamp was turned on...
Incandescent lamp lighting!
...It's warm now!
The incandescent lamp was turned off...
3. Design principles
1 Try to use object aggregation of weak associations and avoid using strong inheritance associations.
2 Abstraction and implementation decoupling.
4. Use occasions
1 Don't want a fixed binding relationship between abstract class and implementation part
The abstraction and implementation parts of 2 classes should be expanded through the method of silencing.
3 Modification of an abstract implementation part does not affect the client, that is, the client code does not have to be recompiled.
5. Bridge mode static class diagram
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.