模板模式以及Spring的回调模式

所谓模板板式,就是在父类中定义算法的主要流程,而把一些个性化的步骤延迟到子类中去实现,父类始终控制着整个流程的主动权,子类只是辅助父类实现某些可定制的步骤。

先用一小段代码回顾模板模式

//模板父类 控制execute的执行流程 而doExecute的具体实现交于之类完成
public abstract class TemplateClass {
    private void before() {
        System.out.println("before execute");
    }
    private void after() {
        System.out.println("after execute");
    }
    protected abstract void doExecute();
    public void execute() {
        before();
        doExecute();
        after();
    }
}
//模板子类 完成doExecute方法的具体实现
public class TemplateSubClass extends TemplateClass{
    @Override
    protected void doExecute() {
        System.out.println("执行 doExecute");
    }
}
//测试结果
public class TestCase {
    public static void main(String[] args) {
        TemplateSubClass subClass = new TemplateSubClass();
        subClass.execute();
    }
}
输出结果
before execute
执行 doExecute
after execute

简单的模板模式回顾完毕,下面通过JDBC的数据库查询讲述Spring的回调模式

//传统的JDBC查询应该是这样
 public List query(String sql) {
        Connection con = DbUtil.openConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try {
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                AppUser appUser = new AppUser();
                appUser.setName(rs.getString("name"));
                appUser.setAge(rs.getInt("age"));
                list.add(appUser);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (pst != null)
                    pst.close();
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

传统的JDBC查询最明显的弊端就是代码重复。可以通过模板模式解决重复代码的问题。

//模板父类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public  abstract class JdbcTemplate {
    public Object query(String sql) {
        Connection con = DbUtil.openConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        Object object = null;
        try {
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            object =  doConvert(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (pst != null)
                    pst.close();
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return object;
    }
    protected abstract Object doConvert(ResultSet resultSet);
}
//数据库查询的所有DAO都继承模板父类,自己实现doConvert方法
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class AppUserDao extends JdbcTemplate {
    @Override
    protected Object doConvert(ResultSet rs) {
        List list = new ArrayList();
        try {
            while (rs.next()) {
                AppUser appUser = new AppUser();
                appUser.setName(rs.getString("name"));
                appUser.setAge(rs.getInt("age"));
                list.add(appUser);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}

上面的模板模式解决了代码重复的问题。在面向对象上的路上又多走了一步。在设计模式里一个很重要的准则聚合优于继承。每一个Dao都必须继承模板,这是不可取的。回调模式就是为了解决这样的问题。

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public abstract class JdbcTemplate {

    private  T execute(StatementCallback callback) throws SQLException {
        Connection con = DbUtil.openConnection();
        Statement pst = null;
        ResultSet rs = null;
        T object = null;
        try {
            pst = con.createStatement();
            object = callback.doStatement(pst);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (pst != null)
                    pst.close();
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return object;
    }

    public  T query(StatementCallback callback) throws SQLException {
        return execute(callback);
    }

    public void delete(final String sql) throws SQLException {
        execute(new StatementCallback() {

            @Override
            public Object doStatement(Statement statement) {
                try {
                    statement.executeUpdate(sql);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

}
 
 
import java.sql.Statement;
public interface StatementCallback {
    T doStatement(Statement statement);
}
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class AppUserDao {
    private JdbcTemplate jdbcTemplate;
    public List query(final String sql) throws SQLException {
        return jdbcTemplate.query(new StatementCallback>() {
            @Override
            public List doStatement(Statement statement) {
                List list = new ArrayList();
                try {
                    ResultSet rs = statement.executeQuery(sql);
                    while (rs.next()) {
                        AppUser appUser = new AppUser();
                        appUser.setName(rs.getString("name"));
                        appUser.setAge(rs.getInt("age"));
                        list.add(appUser);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return list;
            }
        });
    }
}
public void delete(String sql) throws SQLException{
    jdbcTemplate.delete(sql);
}

你可能感兴趣的:(模板模式以及Spring的回调模式)