一模式定义
桥接模式,也称桥梁模式,在软件系统中,由于自身的逻辑,具有两个或多个维度的变化,如何应对这种多维度的变化,桥接模式使得软件系统能够轻松地沿着多个方向进行变化,而又不引入额外的复杂度。
桥接模式三个关键词为:抽象化,实现化,脱耦
二模式举例
1 桥接模式分析方法
我们借用电灯照明来说明该模式。
不使用继承,使用对象组合的方式,将开关和电灯的强关联关系变成弱关联关系。
2桥接模式静态类模型
3代码示例
3.1创建电灯接口
Java代码
packagecom.demo.bridge.lights;
/**
*电灯接口
*
*@author
*
*/
publicinterfaceILight
{
//接通电流
publicvoidelectricConnected();
//照明
publicvoidlight();
//电流关闭
publicvoidelectricClosed();
}
3.2创建一般开关
Java代码
packagecom.demo.bridge.switchs;
importcom.demo.bridge.lights.ILight;
/**
*开关顶层类
*
*@author
*
*/
publicclassBaseSwitch{
//使用组合设置ILight为内部私有属性此为桥梁
protectedILightlight;
//构造方法将外部的light类型注入进来
publicBaseSwitch(ILightlight){
this.light=light;
}
/**
*开灯方法
*/
publicfinalvoidmakeLight(){
//打开开关接通电流
this.light.electricConnected();
//照明
this.light.light();
//关闭开关关闭电流
this.light.electricClosed();
}
}
3.3创建遥控开关
Java代码
packagecom.demo.bridge.switchs.sub;
importcom.demo.bridge.lights.ILight;
importcom.demo.bridge.switchs.BaseSwitch;
/**
*遥控开关继承BaseSwitch扩展功能
*
*@author
*
*/
publicclassRemoteControlSwitchextendsBaseSwitch
{
//构造方法
publicRemoteControlSwitch(ILightlight)
{
super(light);
}
/**
*使用遥控开关控制开灯
*
*@paramoperColor
*灯颜色
*/
publicfinalvoidmakeRemoteLight(intoperColor)
{
//打开开关接通电流
this.light.electricConnected();
//照明
this.light.light();
Stringcolor="";
switch(operColor)
{
case1:
color="暖色";
break;
case2:
color="蓝色";
break;
case3:
color="红色";
break;
default:
color="白色";
break;
}
System.out.println("...现在是"+color+"!");
//关闭开关关闭电流
this.light.electricClosed();
}
}
3.4白炽灯实现
Java代码
packagecom.demo.bridge.lights.impl;
importcom.demo.bridge.lights.ILight;
/**
*白炽灯实现
*
*@author
*
*/
publicclassIncandescentLightimplementsILight
{
//电流关闭
publicvoidelectricClosed()
{
System.out.println("白炽灯被关闭了...");
}
//接通电流
publicvoidelectricConnected()
{
System.out.println("白炽灯被打开了...");
}
//照明
publicvoidlight()
{
System.out.println("白炽灯照明!");
}
}
3.5水晶灯实现
Java代码
packagecom.demo.bridge.lights.impl;
importcom.demo.bridge.lights.ILight;
/**
*水晶灯实现
*
*@author
*
*/
publicclassCrystalLightimplementsILight
{
//电流关闭
publicvoidelectricClosed()
{
System.out.println("水晶灯被关闭了...");
}
//接通电流
publicvoidelectricConnected()
{
System.out.println("水晶灯被打开了...");
}
//照明
publicvoidlight()
{
System.out.println("水晶灯照明!");
}
}
3.6一般开关控制白炽灯,遥控开关控制水晶灯
Java代码
packagecom.demo;
importcom.demo.bridge.lights.ILight;
importcom.demo.bridge.lights.impl.CrystalLight;
importcom.demo.bridge.lights.impl.IncandescentLight;
importcom.demo.bridge.switchs.BaseSwitch;
importcom.demo.bridge.switchs.sub.RemoteControlSwitch;
/**
*客户端应用程序
*
*@author
*
*/
publicclassClientForBridge{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//白炽灯实例
ILightincandescentLight=newIncandescentLight();
//水晶灯实例
ILightcrystalLight=newCrystalLight();
//一般开关
System.out.println("--一般开关--");
BaseSwitchswitch1=newBaseSwitch(incandescentLight);
switch1.makeLight();
System.out.println("\n--遥控开关--");
//遥控开关
RemoteControlSwitchremoteControlSwitch=newRemoteControlSwitch(
crystalLight);
remoteControlSwitch.makeRemoteLight(1);
}
}
运行结果:
-- 一般开关 --
白炽灯被打开了...
白炽灯照明!
白炽灯被关闭了...
-- 遥控开关 --
水晶灯被打开了...
水晶灯照明!
...现在是暖色!
水晶灯被关闭了...
3.7一般开关控制水晶灯,遥控开关控制白炽灯
Java代码
packagecom.demo;
importcom.demo.bridge.lights.ILight;
importcom.demo.bridge.lights.impl.CrystalLight;
importcom.demo.bridge.lights.impl.IncandescentLight;
importcom.demo.bridge.switchs.BaseSwitch;
importcom.demo.bridge.switchs.sub.RemoteControlSwitch;
/**
*客户端应用程序
*
*@author
*
*/
publicclassClientForBridge{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//白炽灯实例
ILightincandescentLight=newIncandescentLight();
//水晶灯实例
ILightcrystalLight=newCrystalLight();
//一般开关
System.out.println("--一般开关--");
BaseSwitchswitch1=newBaseSwitch(crystalLight);
switch1.makeLight();
System.out.println("\n--遥控开关--");
//遥控开关
RemoteControlSwitchremoteControlSwitch=newRemoteControlSwitch(
incandescentLight);
remoteControlSwitch.makeRemoteLight(1);
}
}
运行结果
-- 一般开关 --
水晶灯被打开了...
水晶灯照明!
水晶灯被关闭了...
-- 遥控开关 --
白炽灯被打开了...
白炽灯照明!
...现在是暖色!
白炽灯被关闭了...
三设计原则
1 尽量使用对象聚合弱关联,避免使用继承强关联。
2 抽象化和实现化脱耦。
四使用场合
1不希望在抽象类和实现部分之间有一个固定的绑定关系
2类的抽象及实现部分都应该可以通过孑类的方法加以扩充
3对一个抽象的实现部分的修改对客户不产生影响,即客户代码不必重新编译
五桥接模式静态类图
如果觉得《桥接模式——开关和电灯照明》对你有帮助,请点赞、收藏,并留下你的观点哦!