模板方法模式与回调函数
先上模板方法模式的类图:
代码演示:
public interface StatementCallback{ Object doWithStatement(Statement stmt);}public class JdbcTemplate { public final Object execute(StatementCallback callback){ Connection con=null; Statement stmt=null; try { con=getConnection(); stmt=con.createStatement(); Object retValue=callback.doWithStatement(stmt); return retValue; } catch(SQLException e){ ... } finally { closeStatement(stmt); releaseConnection(con); }}