本次将介绍第一个模式,也是最容易理解和试用的模式之一,模板方法模式,从一个简单场景触发介绍改模式。
讲述DRY原则
模板方法模式
引入回调的模板方法模式
父类代码
package com.model.templateMethod;
/**
* 模板方法
*
* Don't Repeat Yourself
*
* @author leo Chen
*
*/
public abstract class HappyPeople {
public void celebrateSpringFestival() {
// Bying ticket 购买车票
subcribeTicket();
// Travelling by train... 乘坐火车
travel();
// Celebrating Chinese New Year.. 过春节
celebrate();
}
/**
* 庆祝
*/
private void celebrate() {
System.out.println("Celebrating Chinese New Year");
}
/**
* 乘坐交通工具
*/
protected abstract void travel();
/**
* 购买车票
*/
protected final void subcribeTicket() {
System.out.println("Buying ticket..");
}
}
继承的子类PassengerByAir
package com.model.templateMethod;
public class PassengerByAir extends HappyPeople {
@Override
protected void travel() {
// TODO Auto-generated method stub
// Traveling by Air 坐飞机
System.out.println("Traveling by Air...");
}
}
继承的子类PassengerByCoach
package com.model.templateMethod;
public class PassengerByCoach extends HappyPeople {
@Override
protected void travel() {
// TODO Auto-generated method stub
// Traveling by Coach 坐大巴
System.out.println("Traveling by Coach...");
}
}
测试类
package com.model.templateMethod;
public class Test {
public static void main(String[] args) {
HappyPeople passengerByAir = new PassengerByAir();
HappyPeople passengerByCoach = new PassengerByCoach();
System.out.println("Let's Go Home For A Grand Family Reunion....\n");
System.out.println("Tom is going home:");
passengerByAir.celebrateSpringFestival();
System.out.println("\nCatherine is going home");
passengerByCoach.celebrateSpringFestival();
}
}
-
定义在一个操作中的一个算法框架,把一些步骤推迟到子类去实现。模板方法模式让子类不需要概算算法结构而重新定义特定的算法步骤。
回调模式的模板方法,避免子类泛滥
ResultSetHandler
package com.model.templateMethod.callback;
import java.sql.ResultSet;
/**
* 泛型回调类
*
* @author leo Chen
*
* @param <T>
*/
public interface ResultSetHandler<T> {
public T handle(ResultSet rs);
}
SimpleJdbcQueryTemplate
package com.model.templateMethod.callback;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SimpleJdbcQueryTemplate {
public <T> T query(String queryString, ResultSetHandler<T> reHandler) {
Connection connection = null;
PreparedStatement prepareStatement = null;
try {
connection = getCollection();
prepareStatement = connection.prepareStatement(queryString);
ResultSet resultSet = prepareStatement.executeQuery();
return reHandler.handle(resultSet);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
connection.close();
prepareStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
private Connection getCollection() {
Connection connection = null;
try {
connection = DriverManager.getConnection("");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
}
测试类
package com.model.templateMethod.callback;
import java.sql.ResultSet;
public class Test {
public void testTemplate() {
new SimpleJdbcQueryTemplate().query("select * from db",
new ResultSetHandler<Boolean>() {
public Boolean handle(ResultSet rs) {
return Boolean.TRUE;
}
});
}
}