标签:语句 官方 步骤 流程 模板 tco cut rest 模板方法
模板方法是一种行为型模式,执行流程固定,但中间有些步骤有细微差别的。官方的定义是:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。下面使用穷举法举几个例子:
1.西游记里面菩萨定好的 81 难,这就是一个顶层的逻辑骨架。
2.spring 中对 Hibernate 的支持,将一些已经定好的方法封装起来,比如开启事务、获取 Session、关闭 Session 等,程序员不重复写那些已经规范好的代码,直接丢一个实体就可以保存。
下面使用一个例子代码演示一下,主要看一下代码的封装的特点:
package pattern.template.entity; import java.io.Serializable; public class Member implements Serializable { private String username; private String password; private String nickName; private int age; private String addr; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } }
package pattern.template.dao; import pattern.template.JdbcTemplate; import pattern.template.RowMapper; import pattern.template.entity.Member; import java.sql.ResultSet; import java.util.List; public class MemberDao { //像是静态代理 private JdbcTemplate JdbcTemplate=new JdbcTemplate(null); public List<?> query(){ String sql="select * from t_member"; return JdbcTemplate.executeQuery(sql,new RowMapper<Member>(){ @Override public Member mapRow(ResultSet rs, int rowNum) throws Exception{ Member member=new Member(); member.setUsername(rs.getString("username")); member.setPassword(rs.getString("password")); member.setAge(rs.getInt("username")); member.setAddr(rs.getString("addr")); return member; } },null); } }
package pattern.template; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class JdbcTemplate { private DataSource dataSource; public JdbcTemplate(DataSource dataSource){ this.dataSource=dataSource; } private Connection getConnection() throws Exception{ return this.dataSource.getConnection(); } private ResultSet executeQuery(PreparedStatement pstmt,Object[] values) throws Exception{ for (int i=0;i<values.length;i++){ pstmt.setObject(i,values[i]); } return pstmt.executeQuery(); } private PreparedStatement createPreparedStatement(Connection conn,String sql) throws Exception{ return conn.prepareStatement(sql); } private void closeStatement(PreparedStatement pstmt) throws Exception{ pstmt.close(); } private void closeResultSet(ResultSet rs) throws Exception{ rs.close(); } private void closeConnection(Connection conn) throws Exception{ //一般不会关闭 } private List<?> parseResultSet(ResultSet rs,RowMapper rowMapper) throws Exception{ List<Object> result=new ArrayList<>(); int rowNum=1; while (rs.next()){ result.add(rowMapper.mapRow(rs,rowNum++)); } return result; } public List<?> executeQuery(String sql,RowMapper<?> rowMapper,Object[] values) { try { //1、获取连接 Connection conn=this.getConnection(); //2、创建语句集 PreparedStatement pstmt=this.createPreparedStatement(conn,sql); //3、执行语句集,并且获得结果集 ResultSet rs=this.executeQuery(pstmt,values); //4、解析语句集 List<?> result=this.parseResultSet(rs,rowMapper); //5、关闭结果集 this.closeResultSet(rs); //6、关闭语句集 this.closeStatement(pstmt); //7、关闭连接 this.closeConnection(conn); return result; } catch (Exception e) { e.printStackTrace(); } return null; } //public Object processResult(ResultSet rs,int rowNum) throws SQLException; }
package pattern.template; import java.sql.ResultSet; public interface RowMapper<T> { public T mapRow(ResultSet rs,int rowNum) throws Exception; }
package pattern.template; import pattern.template.dao.MemberDao; public class MemberDaoTest { public static void main(String[] args) { MemberDao memberDao = new MemberDao(); memberDao.query(); } }
主要是通过代码深刻的理解代码的封装的优美。
标签:语句 官方 步骤 流程 模板 tco cut rest 模板方法
原文地址:https://www.cnblogs.com/cleveraboy/p/9745252.html