1.JDBC(JavaDatabase Connectivity)数据库
JDBC是以统一方式访问数据库的API.编程
它提供了独立于平台的数据库访问,也就是说,有了JDBC API,咱们就没必要为访问Oracle数据库专门写一个程序,为访问Sybase数据库又专门写一个程序等等,只须要用JDBC API写一个程序就够了,它能够向相应数据库发送SQL调用.JDBC是Java应用程序与各类不一样数据库之间进行对话的方法的机制.简单地说,它作了三件事:与数据库创建链接--发送操做数据库的语句--处理结果.设计模式
jdbc规范使用到主要涉及的设计模式:桥接模式oracle
桥接模式(Bridge)是一种结构型设计模式。Bridge模式基于类的最小设计原则,经过使用封装、聚合及继承等行为让不一样的类承担不一样的职责。它的主要特色是把抽象(Abstraction)与行为实现(Implementation)分离开来,从而能够保持各部分的独立性以及应对他们的功能扩展。ide
桥接模式的角色和职责:测试
1.Client 调用端this
这是Bridge模式的调用者。.net
2.抽象类(Abstraction)设计
抽象类接口(接口这货抽象类)维护队行为实现(implementation)的引用。它的角色就是桥接类。继承
3.Refined Abstraction
这是Abstraction的子类。
4.Implementor
行为实现类接口(Abstraction接口定义了基于Implementor接口的更高层次的操做)
5.ConcreteImplementor
Implementor的子类
桥接模式的UML图以下:
示例代码以下:
首先定义Implementor接口,其中定义了其实现类必需要实现的接口operation()
1 public interface Implementor { 2 public void operation(); 3 }
下面定义Implementor接口的两个实现类:
1 public class ConcreateImplementorA implements Implementor { 2 @Override 3 public void operation() { 4 System.out.println("this is concreteImplementorA's operation..."); 5 } 6 }
1 public class ConcreateImplementorB implements Implementor { 2 @Override 3 public void operation() { 4 System.out.println("this is concreteImplementorB's operation..."); 5 } 6 }
下面定义桥接类Abstraction,其中有对Implementor接口的引用:
1 public abstract class Abstraction { 2 private Implementor implementor; 3 4 public Implementor getImplementor() { 5 return implementor; 6 } 7 8 public void setImplementor(Implementor implementor) { 9 this.implementor = implementor; 10 } 11 12 protected void operation(){ 13 implementor.operation(); 14 } 15 }
下面是Abstraction类的子类RefinedAbstraction:
1 public class RefinedAbstraction extends Abstraction { 2 @Override 3 protected void operation() { 4 super.getImplementor().operation(); 5 } 6 }
下面给出测试类:
1 public class BridgeTest { 2 public static void main(String[] args) { 3 Abstraction abstraction = new RefinedAbstraction(); 4 5 //调用第一个实现类 6 abstraction.setImplementor(new ConcreateImplementorA()); 7 abstraction.operation(); 8 9 //调用第二个实现类 10 abstraction.setImplementor(new ConcreateImplementorB()); 11 abstraction.operation(); 12 13 } 14 }
运行结果以下:
这样,经过对Abstraction桥接类的调用,实现了对接口Implementor的实现类ConcreteImplementorA和ConcreteImplementorB的调用。实现了抽象与行为实现的分离。
总结:
1.桥接模式的优势
(1)实现了抽象和实现部分的分离
桥接模式分离了抽象部分和实现部分,从而极大的提供了系统的灵活性,让抽象部分和实现部分独立开来,分别定义接口,这有助于系统进行分层设计,从而产生更好的结构化系统。对于系统的高层部分,只须要知道抽象部分和实现部分的接口就能够了。
(2)更好的可扩展性
因为桥接模式把抽象部分和实现部分分离了,从而分别定义接口,这就使得抽象部分和实现部分能够分别独立扩展,而不会相互影响,大大的提供了系统的可扩展性。
(3)可动态的切换实现
因为桥接模式实现了抽象和实现的分离,因此在实现桥接模式时,就能够实现动态的选择和使用具体的实现。
(4)实现细节对客户端透明,能够对用户隐藏实现细节。
2.桥接模式的缺点
(1)桥接模式的引入增长了系统的理解和设计难度,因为聚合关联关系创建在抽象层,要求开发者针对抽象进行设计和编程。
(2)桥接模式要求正确识别出系统中两个独立变化的维度,所以其使用范围有必定的局限性。