企业如何通过vue小程序开发满足高效运营与合规性需求
712
2022-11-18
工厂模式之模板模式
文章目录
1.概念2.适用场景3.案例
3.1.面试流程案例
3.1.1.面试流程RecruitProcess3.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中AbstractList4.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
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
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
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小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~