模板模式

编程

模板模式

模板模式又叫做模板方法模式,是指定义一个算法的骨架,并允许子类为一个或者多个步骤提供实现。模板方法使得子类在不改变算法结构的情况下,重新定义算法的某些结构。属于行为性设计模式

适用场景

  1. 一次性实现一个算法的不变的部分,并将可变的行为留给子类来实现。
  2. 各子类中公共的行为被提取并集中到一个公共的父类种中,从而避免大量的重复代码。

缺点

  1. 类数目的增加
  2. 间接的增加了系统的复杂度
  3. 继承关系自身缺点,如果父类增加新的抽象方法,所有子类都要改一遍

JdbcTemplate.class

public abstract class JdbcTemplate {

private DataSource dataSource;

public JdbcTemplate(DataSource dataSource) {

this.dataSource = dataSource;

}

public List<?> executeQuery(String sql, RowMapper<?> mapper, Object[] values) {

try {

Connection conn = this.getConnection();

PreparedStatement pStatement = this.getPrepareStatement(conn, sql);

ResultSet resultSet = this.executeQuery(pStatement, values);

List<?> list = this.parseResultSet(resultSet, mapper);

this.closeResultSet(resultSet);

this.closeStatement(pStatement);

this.closeConnection(conn);

return list;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public Connection getConnection() throws Exception {

return this.dataSource.getConnection();

}

protected PreparedStatement getPrepareStatement(Connection conn, String sql) throws Exception {

return conn.prepareStatement(sql);

}

protected ResultSet executeQuery(PreparedStatement pStatement, Object[] values) throws Exception {

if (values == null) {

return pStatement.executeQuery();

}

for (int i = 0; i < values.length; i++) {

pStatement.setObject(i, values[i]);

}

return pStatement.executeQuery();

}

protected List<?> parseResultSet(ResultSet resultSet, RowMapper<?> mapper) throws Exception {

List<Object> result = new ArrayList<Object>();

int rownum = 1;

while (resultSet.next()) {

result.add(mapper.mapRow(resultSet, rownum++));

}

return result;

}

protected void closeConnection(Connection conn) throws Exception {

conn.close();

}

protected void closeStatement(PreparedStatement pStatement) throws Exception {

pStatement.close();

}

protected void closeResultSet(ResultSet resultSet) throws Exception {

resultSet.close();

}

}

MemberDAO.class

public class MemberDAO extends JdbcTemplate {

public MemberDAO(DataSource dataSource) {

super(dataSource);

}

public List<?> selectAll() {

String sql = "select * from t_user";

return super.executeQuery(sql, new RowMapper<Object>() {

public Object 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("age"));

member.setAddress(rs.getString("address"));

return member;

}

}, null);

}

}

Member.class

public class Member {

private String username;

private String password;

private String nickname;

private int age;

private String address;

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 getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

}

RowMapper.class

public interface RowMapper<T> {

T mapRow(ResultSet rs, int rowNum) throws Exception;

}

以上是 模板模式 的全部内容, 来源链接: utcz.com/z/517424.html

回到顶部