从这个模式开始,都出现在书的附录中,属于不常用的模式。虽然这样,但是我们还是要学习下,哪怕简单地过一下,最起码要知道每个模式的作用是什么。桥接模式的定义是不只改变你的实现,也改变你的抽象。光从定义,不太能够直观地看出这个模式像干啥。下面还是从场景开始分析,慢慢带入进去。
场景描述
假设现在需要制造电视机的遥控器,每个遥控器都有相同的接口,但是每个电视机的实现各不相同。这个场景很简单,使用继承就能很好的解决问题,下面就按照最简单的方式来实现下。
遥控器抽象
1234567public interface RemoteControl {void on();void off();void setChannel(int channel);}
实现RCA电视机对应的遥控器
12345678910111213141516171819public class RCAControl implements RemoteControl {public int channel;public void on() {System.out.println("RCA ON...");}public void off() {System.out.println("RCA OFF...");}public void setChannel(int channel) {this.channel = channel;System.out.println("RCA Tune to channel " + this.channel);}}实现Sony电视机对应的遥控器
12345678910111213141516171819public class SonyControl implements RemoteControl {public int channel;public void on() {System.out.println("Sony ON...");}public void off() {System.out.println("Sony OFF...");}public void setChannel(int channel) {this.channel = channel;System.out.println("SONY Tune to channel " + this.channel);}}测试
12345678910111213public class Test {public static void main(String[] args) {RemoteControl rca = new RCAControl();rca.on();rca.setChannel(9);rca.off();RemoteControl sony = new SonyControl();sony.on();sony.setChannel(12);sony.off();}}
场景跟实现代码都很简单,没什么好说的。但是后面通过用户反馈,需要来持续改进遥控器,这时候因为遥控器和电视机耦合在了一起,改变抽象的同时又要改变实现,就没有任何设计可言了。下面来看下桥接模式如何来应对这个场景。
代码示例
桥接模式通过将实现合抽象放在两个不同的类层次中而使它们可以独立改变。
遥控器抽象电视机抽象
123456789public interface TV {void on();void off();void setChannel(int channel);int getChannel();}遥控器抽象,这个和上面的电视机抽象就是上面说的两个不同的类层次
12345678910111213public abstract class RemoteControl {protected TV tv;public RemoteControl(TV tv) {this.tv = tv;}abstract void on();abstract void off();abstract void setChannel(int channel);}实现两个电视机
123456789101112131415161718192021222324public class RCA implements TV {int channel;public void on() {System.out.println("RCA ON...");}public void off() {System.out.println("RCA OFF...");}public void setChannel(int channel) {this.channel = channel;System.out.println("RCA Tune to channel " + this.channel);}public int getChannel() {return this.channel;}}123456789101112131415161718192021222324public class Sony implements TV {int channel;public void on() {System.out.println("SONY ON...");}public void off() {System.out.println("SONY OFF...");}public void setChannel(int channel) {this.channel = channel;System.out.println("SONY Tune to channel " + this.channel);}public int getChannel() {return this.channel;}}实现遥控器
123456789101112131415161718192021222324252627282930public class ConcreteRemote extends RemoteControl {public ConcreteRemote(TV tv) {super(tv);}public void on() {tv.on();}public void off() {tv.off();}public void setChannel(int channel) {tv.setChannel(channel);}public void nextChannel() {int currentChannel = tv.getChannel();tv.setChannel(++currentChannel);}public void previousChannel() {int currentChannel = tv.getChannel();tv.setChannel(--currentChannel);}}测试
123456789101112131415public class Test {public static void main(String[] args) {RCA rca = new RCA();ConcreteRemote rcaRemote = new ConcreteRemote(rca);rcaRemote.on();rcaRemote.setChannel(9);rcaRemote.off();Sony sony = new Sony();ConcreteRemote sonyRemote = new ConcreteRemote(sony);sonyRemote.on();sonyRemote.setChannel(12);sonyRemote.off();}}
假如现在需要在遥控器上增加上一个节目和下一个节目的功能,下面看下桥接模式如何轻松应对:
修改遥控器实现,添加两个方法实现
123456789101112public class ConcreteRemote implements RemoteControl {......public void nextChannel() {int currentChannel = tv.getChannel();tv.setChannel(++currentChannel);}public void previousChannel() {int currentChannel = tv.getChannel();tv.setChannel(--currentChannel);}}测试
123456789101112131415161718192021public class Test {public static void main(String[] args) {RCA rca = new RCA();ConcreteRemote rcaRemote = new ConcreteRemote(rca);rcaRemote.on();rcaRemote.setChannel(9);rcaRemote.nextChannel();rcaRemote.nextChannel();rcaRemote.previousChannel();rcaRemote.off();Sony sony = new Sony();ConcreteRemote sonyRemote = new ConcreteRemote(sony);sonyRemote.on();sonyRemote.setChannel(12);sonyRemote.previousChannel();sonyRemote.previousChannel();sonyRemote.nextChannel();sonyRemote.off();}}
可见桥接模式确实实现了遥控器界面和电视机的解耦,而且遥控器和电视机可以独立扩展,不会影响到对方。本章节就到这里了。