采用模闆方法模式實作資料庫通路的例子
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();
}
}