定义:是指定义一个算法的骨架,并允许子类为其中一个或者多个步骤提供实现。
模板模式使得子类可以在不改变算法结构的情况下,重新定义算法的某些步骤
(重新定义算法可以实现钩子功能)
适用场景:1、一次性实现一个算法不变的部分,并将可变的行为留给子类来实现。
2、各子类中公共的行为被提取出来并集中到一个公共的父类中,从而避免代码重复
实例:实例中结果集是用户可以参与定义的
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* 模板模式(模板方法模式):有点类似于策略模式,策略模式只有选择权(由用户自己选择已有的固定算法),
* 模板模式侧重的点不是选择,你没得选择,你必须这么做,你可以参与某一部分自定义,
* 整个执行流程固定(例如用户购物,必须是先加入购物车,下单,结算,支付这一套流程),但是用户可以参与
* 其中部分的的过程,结果不固定
*/
public class JdbcTempate {
private DataSource dataSource;
public JdbcTempate(DataSource dataSource){
this.dataSource = dataSource;
}
private Connection getConnection() throws Exception{
return this.dataSource.getConnection();
}
private PreparedStatement createPreparedStatement(Connection conn, String sql) throws Exception{
return conn.prepareStatement(sql);
}
private ResultSet executeQuery(PreparedStatement pstmt, Object[] values) throws Exception{
for(int j = 0; j < values.length; j++){
pstmt.setObject(j, values[j]);
}
return pstmt.executeQuery();
}
private void closeStatment(Statement stmt) throws Exception{
stmt.close();
}
private void closeResult(ResultSet rs) throws Exception{
rs.close();
}
private void closeConnection(Connection conn) throws Exception{
//通常是不关的,而是放回连接池中
//conn.close();
}
private List<?> parseResultSet(ResultSet rs, RowMapper rowMapper) throws Exception{
//返回结果集
List<Object> result = new ArrayList<>();
int rowNum = 1;
while (rs.next()){
result.add(rowMapper.mapperRow(rs, rowNum));
}
return result;
}
public List<?> executeQuery(String sql,RowMapper<?> rowMapper, Object[] values){
//注册驱动]
try {
//获取连接
Connection conn = this.getConnection();
//创建sql对象
PreparedStatement preparedStatement = this.createPreparedStatement(conn, sql);
//执行sql
ResultSet rs = this.executeQuery(preparedStatement, values);
//解析语句集
List<?> result = this.parseResultSet(rs, rowMapper);
//关闭连接
this.closeResult(rs);
this.closeStatment(preparedStatement);
this.closeConnection(conn);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//public abstract Object processResult(ResultSet rs, int rowNum) throws Exception;
}
public class MemberDao {
private JdbcTempate JdbcTempate = new JdbcTempate(null);
public List<?> query(){
String sql = "select * from Member";
//接口实现代替集成抽象类,解耦
//匿名实现
return JdbcTempate.executeQuery(sql, new RowMapper<MemberEntity>() {
@Override
public MemberEntity mapperRow(ResultSet rs, int rowNum) throws Exception {
MemberEntity memberEntity = new MemberEntity();
memberEntity.setUsername(rs.getString("username"));
memberEntity.setAge(rs.getInt("age"));
memberEntity.setPassword(rs.getString("password"));
memberEntity.setNickname(rs.getString("nickname"));
return memberEntity;
}
},null);
}
// @Override
// public Object processResult(ResultSet rs, int rowNum) throws Exception{
// }
}
public class MemberEntity {
private String username;
private String password;
private String nickname;
private int age;
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 interface RowMapper<T> {
T mapperRow(ResultSet rs, int rowNum) throws Exception;
}
public class MemberDaoTest {
public static void main(String[] args) {
MemberDao memberDao = new MemberDao();
memberDao.query();
}
}
网友评论