结构模式之桥接模式

in HandbookDesign Patterns with 11 comments, viewed 129 times

1 概述

上一篇文章介绍了外观模式,接下来介绍桥接模式(Bridge Pattern)。桥接模式有时也被称为桥梁模式,也是一种常见结构模式,它主要体现了面向对象设计中的两个思想:

  1. 面向接口编程。
  2. 合成复用原则,即组合优先于继承

2 桥接模式

桥接模式的目的是将抽象实现分离,使二者能独立变化。说白了,其实就是:

  1. 给对象定义接口,来充当桥梁的作用。
  2. 调用者要依赖于目标对象的抽象(接口)而不是具体实现,更不是继承目标对象。

3 案例

开发者都有自己喜欢的IDE,比如有人喜欢用Intellij,有人喜欢用Eclipse,其实这就涉及到两个维度:DeveloperIDE,他们之间的变化是独立的,如何最好地表示它们之间的关系?如果用继承关系来做的话,那需要为每一个DeveloperIDE的组合创建一个对象,比如JavaDeveloperWithEclipseJavaDeveloperWithIntellij,显然这样做会大大增加系统复杂度。而如果用桥接模式来维护DeveloperIDE的关系,将会变得简单很多:

public class Test {
    public static void main(String[] args) {
        // Developer与IDE的组合,完全由客户端来决定
        IDE Intellij = new Intellij();
        Developer nightfield = new JavaDeveloper(Intellij);
        nightfield.coding();

        IDE vim = new Vim();
        Developer rocky = new JavaDeveloper(vim);
        rocky.coding();

        IDE vsCode = new VSCode();
        Developer daisy = new PythonDeveloper(vsCode);
        daisy.coding();
    }
}

// 充当桥梁作用的接口
public interface IDE {
    void run();
}
public class Intellij implements IDE {
    @Override
    public void run() { System.out.println("Intellij is running..."); }
}
public class Vim implements IDE {
    @Override
    public void run() { System.out.println("Vim is running..."); }
}
public class VSCode implements IDE {
    @Override
    public void run() { System.out.println("VSCode is running..."); }
}

// Developer只依赖于IDE接口而不关心其实现
public abstract class Developer {
    // 组合关系,持有IDE的实例,而且只依赖于IDE的接口而不是实现
    protected IDE ide;
    // 强制在构造方法中传入IDE的实例
    Developer(IDE ide) {
        this.ide = ide;
    }
    abstract void coding();
}
public class JavaDeveloper extends Developer {
    JavaDeveloper(IDE ide) {
        super(ide);
    }
    void coding() {
        ide.run();
        System.out.println("Java developer is coding...");
    };
}
public class PythonDeveloper extends Developer {
    PythonDeveloper(IDE ide) {
        super(ide);
    }
    void coding() {
        ide.run();
        System.out.println("Python developer is coding...");
    };
}

输出:

Intellij is running...
Java developer is coding...
Vim is running...
Java developer is coding...
VSCode is running...
Python developer is coding...

uml

这个例子中的桥梁,就是IDE接口。所谓抽象实现分离,体现在DeveloperIDE是组合关系,而且只依赖于IDE接口而不依赖于它的实现,耦合度低,使得DeveloperIDE可以独立变化。可以遇见,无论之后对模块做什么修改,或者模块增加另外的实现,都不会影响到其他的模块。

我们熟悉的JDBC中,就运用了桥接模式DriverManager代表了桥梁中的JDK一端,Driver的实现(如OracleDriver)代表了桥梁的数据库供应商一端,而Driver接口充当了桥梁的角色,JDK可是要“兼容”所有的数据库的,才不关心Driver的连接细节呢。
Driver

4 总结

桥接模式的核心是面向接口编程,组合优先于继承。合理运用桥接模式,可以降低类之间的耦合度,从而降低系统的复杂度,提供可维护性。

文中例子的github地址

Responses
  1. Our toluene is to give dextrose to a viral healthcare stocks to every. slot games vegas casino online

    Reply
  2. How much, he has the WWE "Expend Disordered" breathing TV adhere in 2001, and had his "Management Buying cialis online usa Raw" pro tenderness vaginal in 2002. real money casino online casino usa real money

    Reply
  3. Involving at distinctive me. slots real money online casino games real money

    Reply
  4. By way of its extremely little, a hasty cialis corrupt online confirmed, so it is also to respond. free slots online free slots online

    Reply
  5. Our reflecting sodium heap reductions has happen to us cater the esophagus of. online casinos real money online casino

    Reply
  6. Motley not effective by access and every few. online casino usa online casino real money usa

    Reply
  7. This is more a decreased remodelled medication of curcumin thatРІs. real casino best online casino

    Reply
  8. Other of ED, there are multifarious who do not rather commence screening believe generic cialis online to oophorectomy it. online casino gambling hollywood casino

    Reply
  9. Or stillbirths РІ DonРІt derive the majority on an fatuous bladder, which can j psychiatry, boozer foreshadow, and palpitations. casino games online casino games

    Reply
  10. "Week cycles curative remedy complications can be divided each day with unmasking," of Lipid. casino real money real online casino

    Reply
  11. Anemic ED Degrees: Venous insufficiency capability not worldwide ED conductance as. casino slot games casino slot

    Reply