zoukankan      html  css  js  c++  java
  • 模板方法模式

    • 模板方法模式介绍

       模板方法模式它定义了一个操作中的算法骨架,将某些步骤延迟到子类中实现。这样,新的子类可以在不改变一个算法结构的前提下重新定义该算法的某些特定步骤。

    • 核心:

      处理某个流程的代码已经都具备,但是其中某个节点的代码暂时不能确定。因此,我们采用工厂方法模式,将这个节点的代码实现转移给子类完成。即:处理步骤父类定义好,具体实现延迟到子类中定义。如JDBC可以采用模板方法,消除部分重复代码。

    • JDBC操作数据库
     1 package templateMethod;
     2 
     3 import java.sql.*;
     4 
     5 /**
     6  * Created by huangyichun on 2017/6/30.
     7  */
     8 public class JDBC {
     9 
    10     public static void main(String[] args) throws ClassNotFoundException, SQLException {
    11         String driver = "com.mysql.jdbc.Driver";
    12         String url = "jdbc:mysql://localhost:3306/test";
    13         String username = "root";
    14         String password = "huangyichun";
    15 
    16         Class.forName(driver);
    17         Connection conn = DriverManager.getConnection(url,username,password);
    18         String sql = "select * from tbl_user";
    19         PreparedStatement statement = conn.prepareStatement(sql);
    20         ResultSet resultSet = statement.executeQuery();
    21         while (resultSet.next()) {
    22             System.out.print(resultSet.getString("userId")+" ");
    23             System.out.print(resultSet.getString("name")+" ");
    24             System.out.print(resultSet.getString("depId")+" ");
    25             System.out.println(resultSet.getString("sex"));
    26         }
    27         conn.close();
    28     }
    29 }
    • 采用模板方法封装后的操作
     1 package templateMethod;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.SQLException;
     6 
     7 /**
     8  * Created by huangyichun on 2017/6/30.
     9  */
    10 public abstract class JDBCTemplateMethod {
    11 
    12 
    13     private Connection getConnection() {
    14         String driver = "com.mysql.jdbc.Driver";
    15         String url = "jdbc:mysql://localhost:3306/test";
    16         String username = "root";
    17         String password = "huangyichun";
    18         Connection conn = null;
    19         try {
    20             Class.forName(driver);
    21             conn = DriverManager.getConnection(url, username, password);
    22         } catch (ClassNotFoundException e) {
    23             e.printStackTrace();
    24         } catch (SQLException e) {
    25             e.printStackTrace();
    26         }
    27         return conn;
    28     }
    29 
    30     public abstract void executeSql(Connection connection);
    31 
    32     public void closeConnection(Connection connection) {
    33         try {
    34             if (connection != null)
    35                 connection.close();
    36         } catch (SQLException e) {
    37             e.printStackTrace();
    38         }
    39     }
    40 
    41     public final void process() {
    42         Connection connection = getConnection();
    43         executeSql(connection);
    44         closeConnection(connection);
    45     }
    46 }

    上面就是一个模板,定义了一个抽象的方法,给子类实现,process方法是核心,该方法使用final修饰,防止子类中进行重写。

     1 package templateMethod;
     2 
     3 import java.sql.Connection;
     4 import java.sql.PreparedStatement;
     5 import java.sql.ResultSet;
     6 import java.sql.SQLException;
     7 
     8 /**
     9  * Created by huangyichun on 2017/6/30.
    10  */
    11 public class JDBCTemplateSelect extends JDBCTemplateMethod{
    12     @Override
    13     public void executeSql(Connection connection) {
    14         String sql = "select * from tbl_user";
    15         PreparedStatement statement = null;
    16 
    17         try {
    18             statement = connection.prepareStatement(sql);
    19             ResultSet resultSet = statement.executeQuery();
    20             while (resultSet.next()) {
    21                 System.out.print(resultSet.getString("userId")+" ");
    22                 System.out.print(resultSet.getString("name")+" ");
    23                 System.out.print(resultSet.getString("depId")+" ");
    24                 System.out.println(resultSet.getString("sex"));
    25             }
    26         } catch (SQLException e) {
    27             e.printStackTrace();
    28         }
    29 
    30 
    31     }
    32 }
     1 package templateMethod;
     2 
     3 /**
     4  * Created by huangyichun on 2017/6/30.
     5  */
     6 public class JDBCClient {
     7     public static void main(String[] args) {
     8         JDBCTemplateSelect select = new JDBCTemplateSelect();
     9         select.process();
    10     }
    11 }

    这里只是举个案例,只是对JDBC进行简单的分装,来解释模板方法模式。

    • 开发中常见的使用场景
    1. 数据库访问的封装
    2. Junit单元测试
    3. servlet中关于doGet/doPost方法调用
    4. Hibernate中模板程序
    5. Spring中JDBCTemplate、HibernateTemplate等
  • 相关阅读:
    Nginx负载均衡+代理+ssl+压力测试
    Nginx配置文件详解
    HDU ACM 1690 Bus System (SPFA)
    HDU ACM 1224 Free DIY Tour (SPFA)
    HDU ACM 1869 六度分离(Floyd)
    HDU ACM 2066 一个人的旅行
    HDU ACM 3790 最短路径问题
    HDU ACM 1879 继续畅通工程
    HDU ACM 1856 More is better(并查集)
    HDU ACM 1325 / POJ 1308 Is It A Tree?
  • 原文地址:https://www.cnblogs.com/huangyichun/p/7097387.html
Copyright © 2011-2022 走看看