1.使用场景
a.一次性实现一个算法的公共部分,将可变部分留给子类
b.各类中的公共方法,剥离变化后进行提取放入共同的父类中
2.DEMO
2.1
/**
* Description:促销抽象类
*
* @date 2019-05-24 19:48
*/
public abstract class Promo {
protected PromoType promoType;
/**
* Description:获取促销信息
* @Date 2019-05-24 19:59
* @Param [promoId]
* @return java.lang.String
**/
public abstract String getPromoInfo(Long promoId);
/**
* Description:获取促销名称
* @Date 2019-05-24 19:48
* @Param []
* @return java.lang.String
**/
protected final String getPromoName(Long promoId){
//调用促销mapper根据id查询促销名称
return "促销名称X";
}
/**
* Description:获取促销范围
* @Date 2019-05-24 19:48
* @Param []
* @return java.lang.String
**/
protected final String getPromoScope(Long promoId){
//调用促销mapper根据id查询促销范围
return "促销范围X";
}
/**
* Description:获取促销标签
* @Date 2019-05-24 19:48
* @Param []
* @return java.lang.String
**/
protected final String getPromoLable(Long promoId){
//调用促销mapper根据id查询促销范围
return "促销标签X";
}
/**
* Description:促销类型枚举
*
* @date 2019-05-24 19:55
*/
public enum PromoType {
DOWN(1,"直降"),
ADD_MONEY_BUY(2,"加价购");
private int typeCode;
private String typename;
PromoType(int typeCode, String typename) {
this.typeCode = typeCode;
this.typename = typename;
}
public int getTypeCode() {
return typeCode;
}
public String getTypename() {
return typename;
}
}
/**
* Description:直降促销
*
* @date 2019-05-24 19:54
*/
public class PromoDown extends Promo{
public PromoDown() {
this.promoType=PromoType.DOWN;
}
@Override
public String getPromoInfo(Long promoId) {
String promoName = this.getPromoName(promoId);
String promoScope = this.getPromoScope(promoId);
String promoLable = this.getPromoLable(promoId);
System.out.println("促销名称="+promoName);
System.out.println("促销范围="+promoScope);
System.out.println("促销标签="+promoLable);
System.out.println("促销类型="+promoType.getTypename());
return null;
}
}
/**
* Description:加价购促销
*
* @date 2019-05-24 19:54
*/
public class AddMoneyBuy extends Promo{
public AddMoneyBuy() {
this.promoType=PromoType.ADD_MONEY_BUY;
}
@Override
public String getPromoInfo(Long promoId) {
String promoName = this.getPromoName(promoId);
String promoLable = this.getPromoLable(promoId);
System.out.println("促销名称="+promoName);
System.out.println("促销范围=全场");
System.out.println("促销标签="+promoLable);
System.out.println("促销类型="+promoType.getTypename());
return null;
}
}
/**
* Description:测试类
*
* @date 2019-05-24 20:06
*/
public class Test {
public static void main(String[] args) {
Promo down = new PromoDown();
Promo addBuy = new AddMoneyBuy();
System.out.println("---->获取直降信息");
down.getPromoInfo(1L);
System.out.println("---->获取加价购信息");
addBuy.getPromoInfo(2L);
}
}
2.2
/**
* Description:ORM映射接口
*
* @date 2019-05-24 20:16
*/
public interface RowMapper<T> {
T mapRow(ResultSet rs,int rowNum) throws Exception;
}
/**
* Description:JDBC模板抽象类
*
* @date 2019-05-24 20:18
*/
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.createPreparedStatement(conn, sql);
//3.执行语句集
ResultSet rs = this.executeQuery(pstm, values);
//4.处理结果集
List<?> result = this.paresResultSet(rs, rowMapper);
//5.关闭结果集
this.closeResultSet(rs);
//6.关于语句集
this.closePreparedStatement(pstm);
//7.关闭连接
this.closeConnection(conn);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
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 pstm, Object[] values) throws Exception {
for(int i=0;i<values.length;i++){
pstm.setObject(i,values[i]);
}
return pstm.executeQuery();
}
private List<?> paresResultSet(ResultSet rs, RowMapper<?> rowMapper) throws Exception {
List<Object> result = new ArrayList<>();
int rowNum = 1;
while (rs.next()){
result.add(rowMapper.mapRow(rs,rowNum++));
}
return result;
}
private void closeResultSet(ResultSet rs) throws Exception {
rs.close();
}
private void closePreparedStatement(PreparedStatement pstm) throws Exception {
pstm.close();
}
private void closeConnection(Connection conn) throws Exception{
conn.close();
}
}
/**
* Description:学生类
*
* @date 2019-05-24 20:48
*/
public class Student {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
* Description:学生dao
*
* @date 2019-05-24 20:49
*/
public class StudentDao extends JdbcTemplate {
public StudentDao(DataSource dataSource) {
super(dataSource);
}
public List<?> selectAll(){
String sql = "select * form student";
return super.executeQuery(sql, new RowMapper<Student>() {
@Override
public Student mapRow(ResultSet rs, int rowNum) throws Exception {
Student st = new Student();
st.setId(rs.getLong("id"));
st.setName(rs.getString("name"));
return st;
}
},null);
}
}