将抽象和实现分离开来,使其分别可以独立变化。

在一个抽象可能有多个实现时,通常使用继承来协调他们。
image.png
这样在扩展Window抽象的时候十分不方便,比如需要增加一个IconWindow抽象时,要增加两个新类:XIconWindow和PMIconWindow
而使用桥接模式可以通过搭配来实现不同功能:将加法变为乘法。

桥接模式的结构

image.png

桥接模式的优点

  1. 接口和实现的分离:一个实现未必不变地绑定在一个接口上。
  2. 提高可扩充性,可以独立的对Abstraction和Implementor层次进行扩充。
  3. 实现细节对客户透明。

代码:

  1. public abstract class TV {
  2. public abstract void on();
  3. public abstract void off();
  4. public abstract void turnChannel();
  5. }
  6. public class Sony extends TV{
  7. @Override
  8. public void on() {
  9. System.out.println("sony.on()");
  10. }
  11. @Override
  12. public void off() {
  13. System.out.println("sony.off()");
  14. }
  15. @Override
  16. public void turnChannel() {
  17. System.out.println("sony.turnChannel()");
  18. }
  19. }
  20. public class RCA extends TV {
  21. @Override
  22. public void on() {
  23. System.out.println("RCA.on()");
  24. }
  25. @Override
  26. public void off() {
  27. System.out.println("RCA.off()");
  28. }
  29. @Override
  30. public void turnChannel() {
  31. System.out.println("RCA.turnChannel()");
  32. }
  33. }
  34. public abstract class RemoteControl {
  35. protected TV tv;
  36. public RemoteControl(TV tv){
  37. this.tv = tv;
  38. }
  39. public abstract void on();
  40. public abstract void off();
  41. public abstract void turnChannel();
  42. }
  43. public class ConcreteRemoteControl extends RemoteControl {
  44. public ConcreteRemoteControl(TV tv) {
  45. super(tv);
  46. }
  47. @Override
  48. public void on() {
  49. System.out.println("ConcreteRemoteControl.on()");
  50. tv.on();
  51. }
  52. @Override
  53. public void off() {
  54. System.out.println("ConcreteRemoteControl.off()");
  55. tv.off();
  56. }
  57. @Override
  58. public void turnChannel() {
  59. System.out.println("ConcreteRemoteControl.turnChannel()");
  60. tv.turnChannel();
  61. }
  62. }
  63. public class ConcreteRemoteControlplus extends RemoteControl {
  64. public ConcreteRemoteControlplus(TV tv) {
  65. super(tv);
  66. }
  67. @Override
  68. public void on() {
  69. System.out.println("ConcreteRemoteControl.on()");
  70. tv.on();
  71. }
  72. @Override
  73. public void off() {
  74. System.out.println("ConcreteRemoteControl.off()");
  75. tv.off();
  76. }
  77. @Override
  78. public void turnChannel() {
  79. System.out.println("ConcreteRemoteControl.turnChannel()");
  80. tv.turnChannel();
  81. }
  82. public void turnOnThe4K(){
  83. System.out.println("turnOnThe4K");
  84. tv.on();
  85. }
  86. }
  87. public class Client {
  88. public static void main(String[] args) {
  89. ConcreteRemoteControl control1 = new ConcreteRemoteControl(new Sony());
  90. control1.on();
  91. control1.off();
  92. ConcreteRemoteControlplus control2 = new ConcreteRemoteControlplus(new RCA());
  93. control2.turnOnThe4K();
  94. control2.off();
  95. }
  96. }

运行结果:

  1. ConcreteRemoteControl.on()
  2. sony.on()
  3. ConcreteRemoteControl.off()
  4. sony.off()
  5. turnOnThe4K
  6. RCA.on()
  7. ConcreteRemoteControl.off()
  8. RCA.off()