一、什么是桥接模式
Bridge 模式又叫做桥接模式,是构造型的设计模式之一。Bridge模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任。它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。
二、桥接模式的结构
三、桥接模式的角色和职责
Client
Bridge模式的使用者
Abstraction
抽象类接口(接口或抽象类)
维护对行为实现(Implementor)的引用
Refined Abstraction
Abstraction子类
Implementor
行为实现类接口 (Abstraction接口定义了基于Implementor接口的更高层次的操作)
ConcreteImplementor
Implementor子类
Car.java
package com.ibeifeng.eg1;
/*
* 汽车
*/
public interface Car {
public void installEngine();
}
Bus.java
package com.ibeifeng.eg1;
public abstract class Bus implements Car {
public abstract void installEngine();
}
Bus2000.java
package com.ibeifeng.eg1;
public class Bus2000 extends Bus{
public void installEngine() {
System.out.println("给Bus安装2000cc发动机");
}
}
Bus2200.java
package com.ibeifeng.eg1;
public class Bus2200 extends Bus {
public void installEngine() {
System.out.println("给Bus安装2200cc发动机");
}
}
Jeep.java
package com.ibeifeng.eg1;
public abstract class Jeep implements Car {
public abstract void installEngine();
}
Jeep2000.java
package com.ibeifeng.eg1;
public class Jeep2000 extends Jeep{
public void installEngine() {
System.out.println("给Jeep安装2000cc发动机");
}
}
Jeep2200.java
package com.ibeifeng.eg1;
public class Jeep2200 extends Jeep {
public void installEngine() {
System.out.println("给Jeep安装2200cc发动机");
}
}
MainClass.java
package com.ibeifeng.eg1;
public class MainClass {
public static void main(String[] args) {
Car car1 = new Bus2000();
car1.installEngine();
}
}
这个方法虽然能够解决问题,但是我们发现这个类非常的多,看第二种解决方案:
Car.java
package com.ibeifeng.eg2;
/*
* 汽车
*/
public interface Car {
public void install2000Engine();
public void install2200Engine();
public void install2300Engine();
}
Bus.java
package com.ibeifeng.eg2;
public class Bus implements Car {
public void install2000Engine() {
System.out.println("给Bus安装2000cc发动机");
}
public void install2200Engine() {
System.out.println("给Bus安装2200cc发动机");
}
public void install2300Engine() {
System.out.println("给Bus安装2400cc发动机");
}
}
MainClass.java
package com.ibeifeng.eg2;
public class MainClass {
public static void main(String[] args) {
Car car1 = new Bus();
car1.install2000Engine();
}
}
这样的设计就可以不再需要那么多子类了,只要把方法写明就可以了,抽象的只有一个,减少了很多子类,但是问题又出现了,比如要新加一个发动机的规格,第一种方案是要增加许多个子类,这里是在Car.java增加一个方法,而且实现类Bus.java也要有具体的实现,这就改变了之前的类,这违反了开放-封闭原则,而且比如有些Bus它没有2000cc的规格,但是Jeep车有,为了保持Car.java这个接口的可重要性,我们要给方法一个空的实现,这也是一个弊端,是非常不好的。这时候我们就可以用桥接模式
Car.java
package com.ibeifeng.eg3;
public abstract class Car {
private Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public abstract void installEngine();
}
Bus.java
package com.ibeifeng.eg3;
public class Bus extends Car{
public Bus(Engine engine) {
super(engine);
}
public void installEngine() {
System.out.print("Bus:");
this.getEngine().installEngine();
}
}
Jeep.java
package com.ibeifeng.eg3;
public class Jeep extends Car {
public Jeep(Engine engine) {
super(engine);
}
public void installEngine() {
System.out.print("Jeep:");
this.getEngine().installEngine();
}
}
Engine.java
package com.ibeifeng.eg3;
public interface Engine {
public void installEngine();
}
Engine2000.java
package com.ibeifeng.eg3;
public class Engine2000 implements Engine {
public void installEngine() {
System.out.println("安装2000cc发动机");
}
}
Engine2200.java
package com.ibeifeng.eg3;
public class Engine2200 implements Engine{
public void installEngine() {
System.out.println("安装2200cc发动机");
}
}
MainClass.java
package com.ibeifeng.eg3;
public class MainClass {
public static void main(String[] args) {
Engine engine2000 = new Engine2000();
Engine engine2200 = new Engine2200();
Car car1 = new Bus(engine2000);
car1.installEngine();
Car car2 = new Bus(engine2200);
car2.installEngine();
Car jeep1 = new Jeep(engine2000);
jeep1.installEngine();
Car jeep2 = new Jeep(engine2200);
jeep2.installEngine();
}
}