模板方法模式
类图
实践1:各个课程流程 求同存异 写法
/** * 模板会有一个或者多个未现实方法, * 而且这几个未实现方法有固定的执行顺序 */ public abstract class NetWorkCourse { protected final void createCourse() { //1、发布预习资料 this.postPreResource(); //2、制作PPT课件 this.createPPT(); //3、在线直播 this.liveVideo(); //4、提交课件、课堂笔记 this.postNote(); //5、提交源码 this.postSource(); //6、布置作业,有些课是没有作业,有些课是有作业的 //如果有作业的话,检查作业,如果没作业,完成了 if (needHomework()) { checkHomework(); } } abstract void checkHomework(); //钩子方法:实现流程的微调 protected boolean needHomework() { return false; } final void postSource() { System.out.println("提交源代码"); } final void postNote() { System.out.println("提交课件和笔记"); } final void liveVideo() { System.out.println("直播授课"); } final void createPPT() { System.out.println("创建备课PPT"); } final void postPreResource() { System.out.println("分发预习资料"); }
public class BigDataCource extends NetWorkCourse { private boolean needHomeworkFlag = false; public BigDataCource(boolean needHomeworkFlag) { this.needHomeworkFlag = needHomeworkFlag; } @Override void checkHomework() { System.out.println("检查大数据的课后作业"); } @Override protected boolean needHomework() { return this.needHomeworkFlag; } }
public class JavaCourse extends NetWorkCourse { @Override void checkHomework() { System.out.println("检查Java的架构课件"); } }
测试NetWorkCourseTest
public class NetWorkCourseTest { public static void main(String[] args) { System.out.println("---检查java作业--"); NetWorkCourse javaCourse = new JavaCourse(); javaCourse.createCourse(); System.out.println("---检查大数据作业--"); BigDataCource bigDataCource = new BigDataCource(true); bigDataCource.createCourse(); } }
可以看到java课程和大数据课程不同的作业,选择实现。相同的逻辑,父类给 实现了。
实践2:使用模板方法模式封装jdbc
public abstract class JdbcTemplate { private DataSource dataSource; public JdbcTemplate(DataSource dataSource) { this.dataSource = dataSource; } public List<?> executeQuery(String sql, RowMapper<?> rowMapper, Object[] values) { try { //1、获取连接 Connection conn = this.getConnection(); //2、创建语句集 PreparedStatement pstm = this.createPrepareStatement(conn, sql); //3、执行语句集 ResultSet rs = this.executeQuery(pstm, values); //4、处理结果集 List<?> result = this.paresResultSet(rs, rowMapper); //5、关闭结果集 this.closeResultSet(rs); //6、关闭语句集 this.closeStatement(pstm); //7、关闭连接 this.closeConnection(conn); return result; } catch (Exception e) { e.printStackTrace(); } return null; } //1、获取连接 public Connection getConnection() throws Exception { return this.dataSource.getConnection(); } //2、创建语句集 protected PreparedStatement createPrepareStatement(Connection conn, String sql) throws Exception { return conn.prepareStatement(sql); } //3、执行语句集 protected ResultSet executeQuery(PreparedStatement pstm, Object[] values) throws Exception { for (int i = 0; i < values.length; i++) { pstm.setObject(i,values[i]); } return pstm.executeQuery(); } //4、处理结果集 protected List<?> paresResultSet(ResultSet rs, RowMapper<?> rowMapper) throws Exception { List<Object> result = new ArrayList<Object>(); int rowNum = 1; while (rs.next()){ result.add(rowMapper.mapRow(rs,rowNum ++)); } return result; } //5、关闭结果集 protected void closeResultSet(ResultSet rs) throws Exception { rs.close(); } //6、关闭语句集 protected void closeStatement(PreparedStatement pstm) throws Exception { pstm.close(); } //7、关闭连接 protected void closeConnection(Connection conn) throws Exception { //数据库连接池,我们不是关闭 conn.close(); } }
public class Member { 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; } }
/** * Title: RowMapper * Description: TODO * * @author hfl * @version V1.0 * @date 2020-05-20 */ public interface RowMapper<T> { T mapRow(ResultSet rs, int rowNum)throws Exception; }
public class MemberDao extends JdbcTemplate { public MemberDao(DataSource dataSource) { super(dataSource); } public List<?> selectAll(){ String sql = "select * from t_member"; return super.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("age")); member.setAddr(rs.getString("addr")); return member; } },null); } }
public class MemberDaoTest { public static void main(String[] args) { MemberDao memberDao = new MemberDao(null); List<?> result = memberDao.selectAll(); System.out.println(result); } }
继续总结下模板方法模式:
好莱坞法则
模板方法模式的本质
什么时候用到模板方法模式
相关模式对比说明
代码地址:
https://github.com/hufanglei/pattern-learn/tree/master/src/main/java/com/example/template