10、桥接模式(Bridge)

来源:互联网 发布:burpsuite mac破解版 编辑:程序博客网 时间:2024/06/08 06:29

10、桥接模式(Bridge)

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

实现代码:

先定义接口:

?
1
2
3
public interface Sourceable {
 publicvoidmethod();
}

 分别定义两个实现类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class SourceSub1 implementsSourceable {
  
 @Override
 publicvoidmethod() {
  System.out.println("this is the first sub!");
 }
}
 
public class SourceSub2 implementsSourceable {
  
 @Override
 publicvoidmethod() {
  System.out.println("this is the second sub!");
 }
}

定义一个桥,持有Sourceable的一个实例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public abstract class Bridge {
 privateSourceable source;
  
 publicvoidmethod(){
  source.method();
 }
   
 publicSourceable getSource() {
  returnsource;
 }
  
 publicvoidsetSource(Sourceable source) {
  this.source = source;
 }
}
 
public class MyBridge extendsBridge {
 publicvoidmethod(){
  getSource().method();
 }
}

测试类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class BridgeTest {
   
 publicstaticvoid main(String[] args) {
    
  Bridge bridge =newMyBridge();
    
  /*调用第一个对象*/
  Sourceable source1 = new SourceSub1();
  bridge.setSource(source1);
  bridge.method();
    
  /*调用第二个对象*/
  Sourceable source2 =newSourceSub2();
  bridge.setSource(source2);
  bridge.method();
 }
}

output:

this is the first sub!
this is the second sub!

这样,就通过对Bridge类的调用,实现了对接口Sourceable的实现类SourceSub1和SourceSub2的调用。接下来我再画个图,大家就应该明白了,因为这个图是我们JDBC连接的原理,有数据库学习基础的,一结合就都懂了。
原创粉丝点击