概述

桥接模式 (Bridge Pattern) 又称柄体 (Handle and Body) 模式、接口模式。它将可能有着继承关系的对象的抽象和实现分离,使得两者可以独立地变化。

优点:分离了抽象部分和实现部分,提高了灵活性和可扩展性,隐藏了实现的细节。
缺点:降低了系统可理解性,提高了设计难度,有一定的局限性。

interface Implementor {
  void a();
}

class Sub1Impl implements Implementor {
  void a() {
    //
  }
}

class Sub2Impl implements Implementor {
  void a() {
    //
  }
}

abstract class Abtract {
  Implementor impl;
  
  abstract void b();
}

class Sub1 extends Abstract {
  b() {
    // other
    impl.a();
    // other
  }
}

class Sub2 extends Abstract {
  b() {
    // other
    impl.a();
    // other
  }
}

图示:
image

参考

[1] 刘伟,设计模式,2011.

 posted on 2023-06-20 11:49  x-yun  阅读(18)  评论(0)    收藏  举报