UP | HOME

桥接模式

Table of Contents

桥接模式就是把事物和其具体实现分开,使他们可以各自独立的变化。用意是:将抽象化与实现化解耦,使得二者可以独立变化,像我们常用的JDBC桥DriverManager一样,JDBC进行连接数据库的时候,在各个数据库之间进行切换,基本不需要动太多的代码,甚至丝毫不用动,原因就是JDBC提供统一接口,每个数据库提供各自的实现,用一个叫做数据库驱动的程序来桥接就行了

UML

bridge.png

Java

  • 定义接口Sourceable:
    public interface Sourceable {
        void method();
    }
    
  • 分别编写两个实现类
    public class SourceSub1 implements Sourceable {
        public void method() {
            System.out.println("this is the first sub!");
        }
    }
    
public class SourceSub2 implements Sourceable {
    public void method() {
        System.out.println("this is the second sub!");
    }
}
  • 定义一个抽象桥,持有Sourceable的一个实例:
    public abstract class AbstractBridge {
        private Sourceable source;
    
        public abstract void method();
    
        public Sourceable getSource() {
            return source;
        }
    
        public void setSource(Sourceable source) {
            this.source = source;
        }
    }
    
  • 实现两个不同的桥接类:
    public class MyBridge1 extends AbstractBridge {
        public void method() {
            System.out.println("my first bridge");
            getSource().method();
        }
    }
    
public class MyBridge2 extends AbstractBridge {
    public void method() {
        System.out.println("my second bridge");
        getSource().method();
    }
}

这样就实现了两个维度的代码的变化,不同的Bridge和不同的Source

  • 测试代码:
    public class MyBridgeTest {
        public static void main(String[] args) {
            AbstractBridge bridge = new MyBridge1();
    
            /*调用第一个对象*/
            Sourceable source1 = new SourceSub1();
            bridge.setSource(source1);
            bridge.method();
    
            /*调用第二个对象*/
            Sourceable source2 = new SourceSub2();
            bridge.setSource(source2);
            bridge.method();
    
            System.out.println();
    
            bridge = new MyBridge2();
            bridge.setSource(source1);
            bridge.method();
            bridge.setSource(source2);
            bridge.method();
    
        }
    }
    
  • 测试结果:
    my first bridge
    this is the first sub!
    my first bridge
    this is the second sub!
    
    my second bridge
    this is the first sub!
    my second bridge
    this is the second sub!
    

Scheme

为brdige,source各自编写一个函数,把这两个函数作为use函数的参数:

(define sub1
  (lambda ()
    (begin
      (display "this is first sub")
      (newline)
      #t)))

(define sub2
  (lambda ()
    (begin
      (display "this is second sub")
      (newline)
      #t)))

(define brg1
  (lambda (sub)
    (begin
      (display "this is first bridge")
      (newline) 
      (sub)
      #t)))

(define brg2
  (lambda (sub)
    (begin
      (display "this is second bridge")
      (newline) 
      (sub)
      #t)))

(define use
  (lambda (bridge)
    (lambda (source)
      (bridge source))))
  • 测试:
    ((use brg1) sub1)
    ((use brg1) sub2)
    ((use brg2) sub1)
    ((use brg2) sub2)
    
    1 ]=> this is first bridge
    this is first sub
    ;Value: #t
    
    1 ]=> this is first bridge
    this is second sub
    ;Value: #t
    
    1 ]=> this is second bridge
    this is first sub
    ;Value: #t
    
    1 ]=> this is second bridge
    this is second sub
    ;Value: #t
    

Next:享元模式

Previous:外观模式

Home:目录