工厂模式之模板模式

网友投稿 712 2022-11-18

工厂模式之模板模式

工厂模式之模板模式

文章目录

​​1.概念​​​​2.适用场景​​​​3.案例​​

​​3.1.面试流程案例​​

​​3.1.1.面试流程RecruitProcess​​​​3.1.2.面试流程CivilianStaffRecruitProcess[文职人员面试流程]​​​​3.1.2.面试流程ItRecruitProcess[IT人员面试流程]​​​​3.1.3.面试流程测试​​

​​3.1.3.1.IT人员测试​​​​3.1.3.2.文职人员测试[不需要笔试]​​

​​3.1.JDBC流程案例​​

​​3.1.1.首先创建 ORM映射定制化的接口​​​​3.1.2.创建JDBC模板​​​​3.1.3.模拟创建数据库实体返回的结果集对象​​​​3.1.4.创建指定实体对象的查询库的DAO对象​​​​3.1.5.测试​​

​​4.补充实际框架的应用举例​​

​​4.1.JDK中AbstractList​​​​4.3.Mybatis中的 BaseExecutor​​

1.概念

1.模板模式通常又称之为模板方法模式,是指定一个一个算法的骨架流程,并允许子类对一个或者多个步骤[方法]提供实现;2.属于行为性设计模式;

2.适用场景

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

3.案例

3.1.面试流程案例

3.1.1.面试流程RecruitProcess

package com.gaoxinfu.demo.parttern.template.recruit;/** * Created by gaoxinfu on 2019/3/17. */public abstract class RecruitProcess { /** * 招聘流程 */ protected final void recruit(){ //1.分析需求人员 this.analyzeRequirements(); //2.发布招聘信息 this.publish(); //3.笔试 if (this.isNeedWrittenExamination()){ this.writtenExamination();; } //4.面试 this.interview(); //5.入职 this.entry(); } /** * 将方法定义为final修饰 主要是指定该方法不能被重写 */ final void analyzeRequirements(){ System.out.println("分析人员需求"); } final void publish(){ System.out.println("发布招聘信息"); } /** * 钩子方法:实现流程的微调 需要实现或者重写的方法,实现流程的微调 * @return */ protected Boolean isNeedWrittenExamination(){ //默认不需要笔试 return false; } /** * 笔试具体根据不同的岗位要求,笔试的内容不一致,因此需要实现 */ abstract void writtenExamination(); final void interview(){ System.out.println("面试"); } final void entry(){ System.out.println("开始入职"); }}

3.1.2.面试流程CivilianStaffRecruitProcess[文职人员面试流程]

package com.gaoxinfu.demo.parttern.template.recruit;/** * Created by gaoxinfu on 2019/3/17. */public class CivilianStaffRecruitProcess extends RecruitProcess{ @Override void writtenExamination() { System.out.println("文职人员不需要笔试"); }}

3.1.2.面试流程ItRecruitProcess[IT人员面试流程]

package com.gaoxinfu.demo.parttern.template.recruit;/** * Created by gaoxinfu on 2019/3/17. */public class ItRecruitProcess extends RecruitProcess{ @Override void writtenExamination() { System.out.println("IT人员笔试流程"); } protected Boolean isNeedWrittenExamination(){ //需要笔试 return true; }}

3.1.3.面试流程测试

3.1.3.1.IT人员测试

package com.gaoxinfu.demo.parttern.template.recruit;/** * Created by gaoxinfu on 2019/3/17. */public class ItRecruitProcessTest { public static void main(String[] args) { RecruitProcess recruitProcess=new ItRecruitProcess(); recruitProcess.recruit(); }}

3.1.3.2.文职人员测试[不需要笔试]

package com.gaoxinfu.demo.parttern.template.recruit;/** * Created by gaoxinfu on 2019/3/17. */public class CivilianStaffRecruitProcessTest { public static void main(String[] args) { RecruitProcess recruitProcess=new CivilianStaffRecruitProcess(); recruitProcess.recruit(); }}

3.1.JDBC流程案例

3.1.1.首先创建 ORM映射定制化的接口

package com.gaoxinfu.demo.parttern.template.jdbc;import java.sql.ResultSet;/** * Created by gaoxinfu on 2019/3/17. * ORM映射定制化的接口 主要是后面映射返回的数据对象 * */public interface RowMapper { /** * @param resultSet * @param rowNum * @return 这里的返回类型T,由入参类型T决定 * @throws Exception */ T mapRow(ResultSet resultSet,int rowNum) throws Exception;}

3.1.2.创建JDBC模板

package com.gaoxinfu.demo.parttern.template.jdbc;import javax.sql.DataSource;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;/** * Created by gaoxinfu on 2019/3/17. */public class JdbcTemplate { private DataSource dataSource; public JdbcTemplate(DataSource dataSource) { this.dataSource = dataSource; } public List executeQuery(String sql,RowMapper rowMapper,Object[] values) throws Exception { /** * 1.获取连接 */ Connection connection=this.getConnection(); //2.创建语句集 PreparedStatement preparedStatement=this.createPrepareStatement(connection,sql); //3.执行语句集 ResultSet resultSet=this.executeQuery(preparedStatement,values); //4.处理结果集 List result=this.parseResultSet(resultSet,rowMapper); //5.关闭结果集 this.closeResultSet(resultSet); //6.关闭语句集 this.closeStatement(preparedStatement); //7.关闭连接 this.closeConnection(connection); return result; } protected Connection getConnection() throws SQLException { return this.dataSource.getConnection(); } protected PreparedStatement createPrepareStatement(Connection conn,String sql) throws SQLException { return conn.prepareStatement(sql); } protected ResultSet executeQuery(PreparedStatement preparedStatement,Object[] values) throws Exception { for (int i = 0;i parseResultSet(ResultSet resultSet,RowMapper rowMapper) throws Exception { List result=new ArrayList<>(); int rowNum=1; while(resultSet.next()){ result.add(rowMapper.mapRow(resultSet,rowNum++)); } return result; } protected void closeStatement(PreparedStatement preparedStatement) throws SQLException { preparedStatement.close(); } protected void closeResultSet(ResultSet resultSet) throws SQLException { resultSet.close();; } protected void closeConnection(Connection conn) throws Exception { //数据库连接池,我们不是关闭 conn.close(); }}

3.1.3.模拟创建数据库实体返回的结果集对象

package com.gaoxinfu.demo.parttern.template.jdbc;/** * Created by gaoxinfu on 2019/3/17. */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; }}

3.1.4.创建指定实体对象的查询库的DAO对象

package com.gaoxinfu.demo.parttern.template.jdbc.dao;import com.gaoxinfu.demo.parttern.template.jdbc.JdbcTemplate;import com.gaoxinfu.demo.parttern.template.jdbc.Member;import com.gaoxinfu.demo.parttern.template.jdbc.RowMapper;import javax.sql.DataSource;import java.sql.ResultSet;import java.util.List;/** * Created by gaoxinfu on 2019/3/17. */public class MemberDAO extends JdbcTemplate{ public MemberDAO(DataSource dataSource) { super(dataSource); } public List selectAll() throws Exception { String sql="select * from t_member"; return super.executeQuery(sql, new RowMapper() { 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); }}

3.1.5.测试

import com.gaoxinfu.demo.parttern.template.jdbc.dao.MemberDAO;import java.util.List;/** * Created by gaoxinfu on 2019/3/17. */public class MemberDaoTest { public static void main(String[] args) throws Exception { MemberDAO memberDAO=new MemberDAO(null); List result=memberDAO.selectAll(); System.out.println(result); }}

由于我们数据库都没有配置,因此,测试肯定不通的,大家理解原理即可

4.补充实际框架的应用举例

4.1.JDK中AbstractList

AbstractList中的get方法等等,由于其实现类不同,实现的方法不一致;

4.3.Mybatis中的 BaseExecutor

java中基础的sql执行类

多个实现类

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:磁盘空间满了故障排查
下一篇:Jmeter BlazeMeter实现web录制过程
相关文章

 发表评论

暂时没有评论,来抢沙发吧~