• 漫说模板方法模式---学生时代的烦恼


    1. 你过了吗?多么令人抓狂的四六级考试呀

     有多少人曾经殚精竭虑?有多少人溯夜难眠?就是为了仅仅通过而已。

    2. 考试之前,匆忙的需找这2B铅笔,考试之中的答题卡,多么熟悉的过程呀,

    多么多的选择题,由我们来涂写,一次次的皱眉,只为胜利的那一刻。

    你看,只有一套题目(其实分AB卷),一套答题纸(也分为AB),多么简单的流程呀,可是为什么我们的分数差别那么大呢??

    4. 下面切入正题,我们的模板方法模式:

    • Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
    • 定义在一个操作中的一个算法框架,把一些步骤推迟到子类去实现。模板方法模式让子类不需要改变算法结构而重新定义特定的算法步骤。

    4. 示例代码

    类图

    template-method-pattern-class-diagram

    package com.programcreek.designpatterns.templatemethod;
     
    abstract public class Vehicle {
        //set to protected so that subclass can access
        protected boolean status;
     
        abstract void start();
        abstract void run();
        abstract void stop();
     
        public void testYourVehicle(){
            start();
            if(this.status){
                run();
                stop();
            }    
        }
    }
    
    package com.programcreek.designpatterns.templatemethod;
     
    public class Car extends Vehicle {
     
        @Override
        void start() {
            this.status = true;
        }
     
        @Override
        void run() {
            System.out.println("Run fast!");
     
        }
     
        @Override
        void stop() {
            System.out.println("Car stop!");
        }
    }
    
    package com.programcreek.designpatterns.templatemethod;
     
    public class Truck extends Vehicle {
     
        @Override
        void start() {
            this.status = true;
        }
     
        @Override
        void run() {
            System.out.println("Run slowly!");
        }
     
        @Override
        void stop() {
            System.out.println("Truck stop!");
     
        }
    }
    
    import com.programcreek.designpatterns.templatemethod.Car;
    import com.programcreek.designpatterns.templatemethod.Truck;
    import com.programcreek.designpatterns.templatemethod.Vehicle;
     
    public class Main {
        public static void main(String args[]){
            Car car = new Car();
            testVehicle(car);
     
            Truck truck = new Truck();
            testVehicle(truck);
        }
     
        public static void testVehicle(Vehicle v){
            v.testYourVehicle();
        }
    }

    5. 应用场景

        1.     多个子类有公共方法,并且逻辑基本相同

        2.    对复杂的算法,核心算法设计为模板方法,细节功能则由各个子类实现

        3.    重构代码

    6.扩展

    1.在spring中的应用spring jdbc

    Spring JDBC提供了一套JDBC抽象框架,用于简化JDBC开发。

    Spring主要提供JDBC模板方式、关系数据库对象化方式、SimpleJdbc方式、事务管理来简化JDBC编程

    Spring提供了3个模板类:

    • JdbcTemplate:Spring里最基本的JDBC模板,利用JDBC和简单的索引参数查询提供对数据库的简单访问。
    • NamedParameterJdbcTemplate:能够在执行查询时把值绑定到SQL里的命名参数,而不是使用索引参数。
    • SimpleJdbcTemplate:利用Java 5的特性,比如自动装箱、通用(generic)和可变参数列表来简化JDBC模板的使用

    2. 在jdk中的应用

    reference:

    http://www.programcreek.com/2012/08/java-design-pattern-template-method/

    http://en.wikipedia.org/wiki/Template_method_pattern

    http://book.51cto.com/art/201202/318456.htm

    http://my.oschina.net/u/1470003/blog/224031

    http://my.oschina.net/aps/blog/85981

    http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns

    注:以上图片来自于互联网,不一一注明了。

  • 相关阅读:
    JPA的查询语言—使用构造器
    Servlet3.0使用注解定义Servlet
    jQuery操作<input type="radio">
    JPA的查询语言—使用原生SQL
    jQuery动态添加<input type="file">
    JPA的查询语言—JPQL的关联查询
    jQuery操作<select>
    Servlet3.0异步处理
    jQuery操作<input type="checkbox">
    mysql binlog二进制日志详解
  • 原文地址:https://www.cnblogs.com/davidwang456/p/3678106.html
Copyright © 2020-2023  润新知