[原创]设计模型探索P-template method parttern之死模活样
模板方法
定义一个操作算法的骨架,而将一些步骤沿迟到子类中去实现,模板方法使了类可能不改变算法的结构即可以重定义该算法的某一些特定步骤.
组成部份
1.抽象模板 是一个抽象类,包含一系列的算法和一个模板方法,模板方法需要加final来修饰.
2.具体模板 继承自抽象类.
各组成部份之间的关系
适用场景:
设计者需要给出一个算法的固定步骤,并将某一些步骤的具体实现留给子类来实现.
需要对代码进行重构,将各个子类的公共行为取出来放到一个共同的父亲类中以避免代码重复
例子:
1.抽象模板
2.具体模板1
3.具体模板2
4.测试类
定义一个操作算法的骨架,而将一些步骤沿迟到子类中去实现,模板方法使了类可能不改变算法的结构即可以重定义该算法的某一些特定步骤.
组成部份
1.抽象模板 是一个抽象类,包含一系列的算法和一个模板方法,模板方法需要加final来修饰.
2.具体模板 继承自抽象类.
各组成部份之间的关系
适用场景:
设计者需要给出一个算法的固定步骤,并将某一些步骤的具体实现留给子类来实现.
需要对代码进行重构,将各个子类的公共行为取出来放到一个共同的父亲类中以避免代码重复
例子:
1.抽象模板
package
template;
/** */ /**
* <ul>
* <li>Title:[AbstractTemplate]</li>
* <li>Description: [抽象模板]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public abstract class DatabaseExecuteTemplate
{
/** *//**
* <ul>
* <li>Description:[加载驱动]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void loadDrive();
/** *//**
* <ul>
* <li>Description:[连接数据库]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void connectDataBase();
/** *//**
* <ul>
* <li>Description:[创建statement]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void createStatement();
/** *//**
* <ul>
* <li>Description:[执行sql]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void executeSQL(String sql);
/** *//**
* <ul>
* <li>Description:[关闭statement]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void closeStatement();
/** *//**
* <ul>
* <li>Description:[关闭连接]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void closeConnect();
/** *//**
* <ul>
* <li>Description:[是否刷新勾子]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @return
*/
public abstract boolean isFresh();
/** *//**
* <ul>
* <li>Description:[刷新]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @return
*/
public abstract void fresh();
/** *//**
* <ul>
* <li>Description:[执行sql]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @param sql
*/
public final void exceute(String sql)
{
// 加载驱动
this.loadDrive();
// 连接数据库
this.connectDataBase();
// 创建statement
this.createStatement();
// 执行sql
this.executeSQL(sql);
//关闭statement
this.closeStatement();
// 关系连接
this.closeConnect();
// 使用勾子改变算法
if(isFresh())
{
// 刷新操作
this.fresh();
}
}
}
/** */ /**
* <ul>
* <li>Title:[AbstractTemplate]</li>
* <li>Description: [抽象模板]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public abstract class DatabaseExecuteTemplate
{
/** *//**
* <ul>
* <li>Description:[加载驱动]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void loadDrive();
/** *//**
* <ul>
* <li>Description:[连接数据库]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void connectDataBase();
/** *//**
* <ul>
* <li>Description:[创建statement]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void createStatement();
/** *//**
* <ul>
* <li>Description:[执行sql]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void executeSQL(String sql);
/** *//**
* <ul>
* <li>Description:[关闭statement]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void closeStatement();
/** *//**
* <ul>
* <li>Description:[关闭连接]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void closeConnect();
/** *//**
* <ul>
* <li>Description:[是否刷新勾子]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @return
*/
public abstract boolean isFresh();
/** *//**
* <ul>
* <li>Description:[刷新]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @return
*/
public abstract void fresh();
/** *//**
* <ul>
* <li>Description:[执行sql]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @param sql
*/
public final void exceute(String sql)
{
// 加载驱动
this.loadDrive();
// 连接数据库
this.connectDataBase();
// 创建statement
this.createStatement();
// 执行sql
this.executeSQL(sql);
//关闭statement
this.closeStatement();
// 关系连接
this.closeConnect();
// 使用勾子改变算法
if(isFresh())
{
// 刷新操作
this.fresh();
}
}
}
2.具体模板1
package
template;
/** */ /**
* <ul>
* <li>Title:[OracleExecuteTemple]</li>
* <li>Description: [oracle模板]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public class OracleExecuteTemple extends DatabaseExecuteTemplate
{
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#closeConnect()
*/
public void closeConnect()
{
// TODO Auto-generated method stub
System.out.println("oracle 关闭连接");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#closeStatement()
*/
public void closeStatement()
{
// TODO Auto-generated method stub
System.out.println("oracle 关闭statement");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#connectDataBase()
*/
public void connectDataBase()
{
// TODO Auto-generated method stub
System.out.println("oracle 连接数据库");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#createStatement()
*/
public void createStatement()
{
// TODO Auto-generated method stub
System.out.println("oracle 创建statement");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#executeSQL()
*/
public void executeSQL(String sql)
{
// TODO Auto-generated method stub
System.out.println("oracle 执行SQL: " + sql);
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#fresh()
*/
public void fresh()
{
System.out.println("oracle 刷新");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#isFresh()
*/
public boolean isFresh()
{
return false;
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#loadDrive()
*/
public void loadDrive()
{
System.out.println("oracle 加载驱动");
}
}
/** */ /**
* <ul>
* <li>Title:[OracleExecuteTemple]</li>
* <li>Description: [oracle模板]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public class OracleExecuteTemple extends DatabaseExecuteTemplate
{
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#closeConnect()
*/
public void closeConnect()
{
// TODO Auto-generated method stub
System.out.println("oracle 关闭连接");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#closeStatement()
*/
public void closeStatement()
{
// TODO Auto-generated method stub
System.out.println("oracle 关闭statement");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#connectDataBase()
*/
public void connectDataBase()
{
// TODO Auto-generated method stub
System.out.println("oracle 连接数据库");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#createStatement()
*/
public void createStatement()
{
// TODO Auto-generated method stub
System.out.println("oracle 创建statement");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#executeSQL()
*/
public void executeSQL(String sql)
{
// TODO Auto-generated method stub
System.out.println("oracle 执行SQL: " + sql);
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#fresh()
*/
public void fresh()
{
System.out.println("oracle 刷新");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#isFresh()
*/
public boolean isFresh()
{
return false;
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#loadDrive()
*/
public void loadDrive()
{
System.out.println("oracle 加载驱动");
}
}
3.具体模板2
package
template;
/** */ /**
* <ul>
* <li>Title:[DB2ExecuteTemple]</li>
* <li>Description: [DB2模板]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public class DB2ExecuteTemple extends DatabaseExecuteTemplate
{
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#closeConnect()
*/
public void closeConnect()
{
// TODO Auto-generated method stub
System.out.println("DB2 关闭连接");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#closeStatement()
*/
public void closeStatement()
{
// TODO Auto-generated method stub
System.out.println("DB2 关闭statement");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#connectDataBase()
*/
public void connectDataBase()
{
// TODO Auto-generated method stub
System.out.println("DB2 连接数据库");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#createStatement()
*/
public void createStatement()
{
// TODO Auto-generated method stub
System.out.println("DB2 创建statement");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#executeSQL()
*/
public void executeSQL(String sql)
{
// TODO Auto-generated method stub
System.out.println("DB2 执行SQL: " + sql);
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#fresh()
*/
public void fresh()
{
System.out.println("DB2 刷新");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#isFresh()
*/
public boolean isFresh()
{
return true;
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#loadDrive()
*/
public void loadDrive()
{
System.out.println("DB2 加载驱动");
}
}
/** */ /**
* <ul>
* <li>Title:[DB2ExecuteTemple]</li>
* <li>Description: [DB2模板]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public class DB2ExecuteTemple extends DatabaseExecuteTemplate
{
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#closeConnect()
*/
public void closeConnect()
{
// TODO Auto-generated method stub
System.out.println("DB2 关闭连接");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#closeStatement()
*/
public void closeStatement()
{
// TODO Auto-generated method stub
System.out.println("DB2 关闭statement");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#connectDataBase()
*/
public void connectDataBase()
{
// TODO Auto-generated method stub
System.out.println("DB2 连接数据库");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#createStatement()
*/
public void createStatement()
{
// TODO Auto-generated method stub
System.out.println("DB2 创建statement");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#executeSQL()
*/
public void executeSQL(String sql)
{
// TODO Auto-generated method stub
System.out.println("DB2 执行SQL: " + sql);
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#fresh()
*/
public void fresh()
{
System.out.println("DB2 刷新");
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#isFresh()
*/
public boolean isFresh()
{
return true;
}
/**//*
* (non-Javadoc)
*
* @see template.DatabaseExecuteTemplate#loadDrive()
*/
public void loadDrive()
{
System.out.println("DB2 加载驱动");
}
}
4.测试类
package
template;
public class Test
{
public static void main(String[] args)
{
DatabaseExecuteTemplate db2 = new DB2ExecuteTemple();
db2.exceute("select count(*) from users");
OracleExecuteTemple oracle = new OracleExecuteTemple();
oracle.exceute("select count(*) from users");
}
}
public class Test
{
public static void main(String[] args)
{
DatabaseExecuteTemplate db2 = new DB2ExecuteTemple();
db2.exceute("select count(*) from users");
OracleExecuteTemple oracle = new OracleExecuteTemple();
oracle.exceute("select count(*) from users");
}
}