[置顶] 桥接模式——开关和电灯照明
来源:互联网 发布:网络文明调查报告总结 编辑:程序博客网 时间:2024/04/29 00:25
一模式定义
桥接模式,也称桥梁模式,在软件系统中,由于自身的逻辑,具有两个或多个维度的变化,如何应对这种多维度的变化,桥接模式使得软件系统能够轻松地沿着多个方向进行变化,而又不引入额外的复杂度。
桥接模式三个关键词为:抽象化,实现化,脱耦
二模式举例
1 桥接模式分析方法
我们借用电灯照明来说明该模式。
不使用继承,使用对象组合的方式,将开关和电灯的强关联关系变成弱关联关系。
2桥接模式静态类模型
3代码示例
3.1创建电灯接口
package com.demo.bridge.lights;/** * 电灯接口 * * @author * */public interface ILight{// 接通电流public void electricConnected();// 照明public void light();// 电流关闭public void electricClosed();}
3.2创建一般开关
package com.demo.bridge.switchs;import com.demo.bridge.lights.ILight;/** * 开关顶层类 * * @author * */public class BaseSwitch {// 使用组合 设置ILight为内部私有属性 此为桥梁protected ILight light;// 构造方法将 外部的light类型注入进来public BaseSwitch(ILight light) {this.light = light;}/** * 开灯方法 */public final void makeLight() {// 打开开关 接通电流this.light.electricConnected();// 照明this.light.light();// 关闭开关 关闭电流this.light.electricClosed();}}
3.3创建遥控开关
package com.demo.bridge.switchs.sub;import com.demo.bridge.lights.ILight;import com.demo.bridge.switchs.BaseSwitch;/** * 遥控开关 继承BaseSwitch 扩展功能 * * @author * */public class RemoteControlSwitch extends BaseSwitch{// 构造方法public RemoteControlSwitch(ILight light){super(light);}/** * 使用遥控开关控制开灯 * * @param operColor * 灯颜色 */public final void makeRemoteLight(int operColor){// 打开开关 接通电流this.light.electricConnected();// 照明this.light.light();String color = "";switch (operColor){case 1:color = "暖色";break;case 2:color = "蓝色";break;case 3:color = "红色";break;default:color = "白色";break;}System.out.println(" ...现在是" + color + "!");// 关闭开关 关闭电流this.light.electricClosed();}}
3.4白炽灯实现
package com.demo.bridge.lights.impl;import com.demo.bridge.lights.ILight;/** * 白炽灯 实现 * * @author * */public class IncandescentLight implements ILight{// 电流关闭public void electricClosed(){System.out.println("白炽灯被关闭了...");}// 接通电流public void electricConnected(){System.out.println("白炽灯被打开了...");}// 照明public void light(){System.out.println("白炽灯照明!");}}
3.5水晶灯实现
package com.demo.bridge.lights.impl;import com.demo.bridge.lights.ILight;/** * 水晶灯 实现 * * @author * */public class CrystalLight implements ILight{// 电流关闭public void electricClosed(){System.out.println("水晶灯被关闭了...");}// 接通电流public void electricConnected(){System.out.println("水晶灯被打开了...");}// 照明public void light(){System.out.println("水晶灯照明!");}}
3.6一般开关控制白炽灯,遥控开关控制水晶灯
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;/** * 客户端应用程序 * * @author * */public class ClientForBridge {/** * @param args */public static void main(String[] args) {// 白炽灯 实例ILight incandescentLight = new IncandescentLight();// 水晶灯 实例ILight crystalLight = new CrystalLight();// 一般开关System.out.println("-- 一般开关 -- ");BaseSwitch switch1 = new BaseSwitch(incandescentLight);switch1.makeLight();System.out.println("\n-- 遥控开关 -- ");// 遥控开关RemoteControlSwitch remoteControlSwitch = new RemoteControlSwitch(crystalLight);remoteControlSwitch.makeRemoteLight(1);}}
运行结果:
-- 一般开关 --
白炽灯被打开了...
白炽灯照明!
白炽灯被关闭了...
-- 遥控开关 --
水晶灯被打开了...
水晶灯照明!
...现在是暖色!
水晶灯被关闭了...
3.7一般开关控制水晶灯,遥控开关控制白炽灯
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;/** * 客户端应用程序 * * @author * */public class ClientForBridge {/** * @param args */public static void main(String[] args) {// 白炽灯 实例ILight incandescentLight = new IncandescentLight();// 水晶灯 实例ILight crystalLight = new CrystalLight();// 一般开关System.out.println("-- 一般开关 -- ");BaseSwitch switch1 = new BaseSwitch(crystalLight);switch1.makeLight();System.out.println("\n-- 遥控开关 -- ");// 遥控开关RemoteControlSwitch remoteControlSwitch = new RemoteControlSwitch(incandescentLight);remoteControlSwitch.makeRemoteLight(1);}}
运行结果
-- 一般开关 --
水晶灯被打开了...
水晶灯照明!
水晶灯被关闭了...
-- 遥控开关 --
白炽灯被打开了...
白炽灯照明!
...现在是暖色!
白炽灯被关闭了...
三设计原则
1 尽量使用对象聚合弱关联,避免使用继承强关联。
2 抽象化和实现化脱耦。
四使用场合
1不希望在抽象类和实现部分之间有一个固定的绑定关系
2类的抽象及实现部分都应该可以通过孑类的方法加以扩充
3对一个抽象的实现部分的修改对客户不产生影响,即客户代码不必重新编译
五桥接模式静态类图