设计模式之桥接模式
Bridge模式有叫做桥接模式,是构造型的设计模式之一。Bridge模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类型承担不同的责任。它的主要特点是把抽象与行为实现分离开来,从二可以保持各部分的独立性以及应对他们的功能扩展。
下面通过,给车子安装发动机为例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge; 2 3 public class Bus2200 extends Bus { 4 5 @Override 6 public void installEngine() { 7 System.out.println("安装2200bus"); 8 } 9 10 }
(一)通过接口Car,拥有安装发动机的方法,然后不同的实现类,安装不同规格的引擎
Car接口:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge; 2 3 /** 4 * @ClassName: Car 5 * @Description:汽车 6 * @author 7 * @date 2018年4月10日 下午10:31:35 8 * @version 1.0 9 */ 10 public interface Car { 11 12 public void installEngine(); 13 14 }
汽车Bus:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge; 2 3 public abstract class Bus implements Car { 4 5 public abstract void installEngine(); 6 7 }
第一种引擎的汽车Bus2000:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge; 2 3 public class Bus2000 extends Bus { 4 5 @Override 6 public void installEngine() { 7 System.out.println("安装引擎2000bus"); 8 } 9 10 }
第二种引擎的汽车Bus2200:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge; 2 3 public class Bus2200 extends Bus { 4 5 @Override 6 public void installEngine() { 7 System.out.println("安装2200bus"); 8 } 9 10 }
吉普Jeep:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge; 2 3 public abstract class Jeep implements Car { 4 5 @Override 6 public abstract void installEngine(); 7 8 }
第一种引擎吉普Jeep2000:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge; 2 3 public class Jeep2000 extends Jeep { 4 5 @Override 6 public void installEngine() { 7 System.out.println("安装2000cc发动机"); 8 } 9 10 }
第二种引擎吉普Jeep2200:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge; 2 3 public class Jeep2200 extends Jeep { 4 5 @Override 6 public void installEngine() { 7 System.out.println("安装2200cc发动机"); 8 } 9 10 }
可以看到:汽车和吉普车安装不同的引擎,都要实现类似的类。这里汽车和吉普用的引擎都是一样的,其实可以把引擎抽象出来(在第三个方式中)。
(二)将安装不同规格的引擎封装成不同的方法,汽车可以有多个安装方法。
汽车Car:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge1; 2 3 public interface Car { 4 5 public void install2000Engine(); 6 public void install2200Engine(); 7 public void install2300Engine(); 8 9 }
实现类Bus:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge1; 2 3 public class Bus implements Car { 4 5 @Override 6 public void install2000Engine() { 7 System.out.println("給bus安裝2000cc发动机"); 8 } 9 10 @Override 11 public void install2200Engine() { 12 System.out.println("給bus安裝2200cc发动机"); 13 } 14 15 @Override 16 public void install2300Engine() { 17 System.out.println("給bus安裝2300cc发动机"); 18 } 19 20 }
这个明显不符合开放闭合原则,若是增加一种引擎,就要改动类。
(三)将引擎和车子分离,然后将安装选择延迟给客户端进行选择
引擎Engine:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge2; 2 3 public interface Engine { 4 5 public void installEngine(); 6 7 }
第一种规格的引擎Engin2000:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge2; 2 3 public class Engine2000 implements Engine { 4 5 @Override 6 public void installEngine() { 7 System.out.println("安装2000cc发动机"); 8 } 9 10 }
第二种规格的引擎Engin2200:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge2; 2 3 public class Engine2200 implements Engine { 4 5 @Override 6 public void installEngine() { 7 System.out.println("安装2200cc发动机"); 8 } 9 10 }
车子Car(持有引擎的引用):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge2; 2 3 public abstract class Car { 4 5 private Engine engine; 6 7 public Car(Engine engine){ 8 this.engine = engine; 9 } 10 11 public abstract void installEngine(); 12 13 public Engine getEngine() { 14 return engine; 15 } 16 17 public void setEngine(Engine engine) { 18 this.engine = engine; 19 } 20 21 22 }
汽车Bus:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge2; 2 3 public class Bus extends Car { 4 5 public Bus(Engine engine) { 6 super(engine); 7 } 8 9 @Override 10 public void installEngine() { 11 System.out.println("Bus:"); 12 this.getEngine().installEngine(); 13 } 14 15 }
吉普Jeep:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Design.Bridge2; 2 3 public class Jeep extends Car { 4 5 public Jeep(Engine engine) { 6 super(engine); 7 } 8 9 @Override 10 public void installEngine() { 11 System.out.println("Jeep:"); 12 this.getEngine().installEngine(); 13 } 14 15 }
可以看出来:客户端需要安装什么样的引擎,只要准备扩展引擎类就行了。