桥接模式

1. 什么是桥接模式

Bridge 模式又叫做桥接模式,是构造型的设计模式之一。 Bridge 模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任。它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。

2. 桥接模式的结构

12_桥接模式 - 图1

3. 代码演示

3.1. car1

  1. public interface Car {
  2. void installEngine();
  3. }
  1. public abstract class Bus implements Car {
  2. @Override
  3. public abstract void installEngine();
  4. }
  1. public class Bus2000 extends Bus {
  2. @Override
  3. public void installEngine() {
  4. System.out.println("给Bus安装2000cc发动机");
  5. }
  6. }
  1. public class Bus2200 extends Bus {
  2. @Override
  3. public void installEngine() {
  4. System.out.println("给Bus安装2200cc发动机");
  5. }
  6. }
  1. public abstract class Jeep implements Car {
  2. @Override
  3. public abstract void installEngine();
  4. }
  1. public class Jeep2000 extends Bus {
  2. @Override
  3. public void installEngine() {
  4. System.out.println("给Jeep安装2000cc发动机");
  5. }
  6. }
  1. public class Jeep2200 extends Bus {
  2. @Override
  3. public void installEngine() {
  4. System.out.println("给Jeep安装2200cc发动机");
  5. }
  6. }
  1. Car bus2000 = new Bus2000();
  2. bus2000.installEngine();
  3. Car bus2200 = new Bus2200();
  4. bus2200.installEngine();
  5. Car jeep2000 = new Jeep2000();
  6. jeep2000.installEngine();
  7. Car jeep2200 = new Jeep2200();
  8. jeep2200.installEngine();
  1. Bus安装2000cc发动机
  2. Bus安装2200cc发动机
  3. Jeep安装2000cc发动机
  4. Jeep安装2200cc发动机

3.2. car2

  1. public interface Car {
  2. void install2000Engine();
  3. void install2200Engine();
  4. void install2400Engine();
  5. }
  1. public class Bus implements Car {
  2. @Override
  3. public void install2000Engine() {
  4. System.out.println("给Bus安装2000cc发动机");
  5. }
  6. @Override
  7. public void install2200Engine() {
  8. System.out.println("给Bus安装2200cc发动机");
  9. }
  10. @Override
  11. public void install2400Engine() {
  12. System.out.println("给Bus安装2400cc发动机");
  13. }
  14. }
  1. Car bus = new Bus();
  2. bus.install2000Engine();
  3. bus.install2200Engine();
  4. bus.install2400Engine();
  1. Bus安装2000cc发动机
  2. Bus安装2200cc发动机
  3. Bus安装2400cc发动机

3.3. car3

  1. public interface Engine {
  2. void displayEngine();
  3. }
  1. public class Engine2000 implements Engine {
  2. @Override
  3. public void displayEngine() {
  4. System.out.println("安装2000cc发动机");
  5. }
  6. }
  1. public class Engine2200 implements Engine {
  2. @Override
  3. public void displayEngine() {
  4. System.out.println("安装2200cc发动机");
  5. }
  6. }
  1. public abstract class Car {
  2. private final Engine engine;
  3. public Car(Engine engine) {
  4. this.engine = engine;
  5. }
  6. public Engine getEngine() {
  7. return engine;
  8. }
  9. public abstract void installEngine();
  10. }
  1. public class Bus extends Car {
  2. public Bus(Engine engine) {
  3. super(engine);
  4. }
  5. @Override
  6. public void installEngine() {
  7. System.out.print("Bus:");
  8. this.getEngine().displayEngine();
  9. }
  10. }
  1. public class Jeep extends Car {
  2. public Jeep(Engine engine) {
  3. super(engine);
  4. }
  5. @Override
  6. public void installEngine() {
  7. System.out.print("Jeep:");
  8. this.getEngine().displayEngine();
  9. }
  10. }
  1. Engine engine2000 = new Engine2000();
  2. Engine engine2200 = new Engine2200();
  3. Car bus1 = new Bus(engine2000);
  4. bus1.installEngine();
  5. Car bus2 = new Bus(engine2200);
  6. bus2.installEngine();
  7. Car jeep1 = new Jeep(engine2000);
  8. jeep1.installEngine();
  9. Car jeep2 = new Jeep(engine2200);
  10. jeep2.installEngine();
  1. Bus:安装2000cc发动机
  2. Bus:安装2200cc发动机
  3. Jeep:安装2000cc发动机
  4. Jeep:安装2200cc发动机

3.4. 新增 Truck

  1. public class Truck extends Car {
  2. public Truck(Engine engine) {
  3. super(engine);
  4. }
  5. @Override
  6. public void installEngine() {
  7. System.out.print("Truck:");
  8. this.getEngine().displayEngine();
  9. }
  10. }
  1. Car truck1 = new Truck(engine2000);
  2. truck1.installEngine();
  3. Car truck2 = new Truck(engine2200);
  4. truck2.installEngine();
  1. Truck:安装2000cc发动机
  2. Truck:安装2200cc发动机