采用模板方法模式实现数据库访问的例子
DataObject.java
package 模板方法;
public abstract class DataObject {
protected abstract void startConnection();
protected abstract void endConnect();
public void templateMethod() {
this.startConnection();
this.endConnect();
}
}
package 模板方法;
public class CustomerDataObject extends DataObject {
@Override
protected void startConnection() {
System.out.println("已连接数据库");
}
@Override
protected void endConnect() {
System.out.println("已关闭数据库");
}
}
package 模板方法;
public class Client {
public static void main(String[] args) {
DataObject user=new CustomerDataObject();
user.templateMethod();
}
}