假设你发现你已经非常重码,你可能会考虑使用模板的方法来消除easy重复错误代码。下面是一个示例:以下两类,他完成了几乎相同的功能:

  1. 实例化并初始化一个Reader来读取CSV文件。
  2. 读取每一行并解析;
  3. 把每一行的字符填充到Product或Customer对象;
  4. 将每个对象加入到Set里;
  5. 返回Set。

正如你看到的,仅仅有有凝视的地方是不一样的。其它全部步骤都是同样的。

ProductCsvReader.java

public class ProductCsvReader {
 
    Set<Product> getAll(File file) throws IOException {
        Set<Product> returnSet = new HashSet<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(file))){
            String line = reader.readLine();
            while (line != null && !line.trim().equals("")) {
                String[] tokens = line.split("\\s*,\\s*");
                //不同
                Product product = new Product(Integer.parseInt(tokens[0]), tokens[1], new BigDecimal(tokens[2]));
                returnSet.add(product);
                line = reader.readLine();
            }
        }
        return returnSet;
    }
}

CustomerCsvReader.java

public class CustomerCsvReader {
 
    Set<Customer> getAll(File file) throws IOException {
        Set<Customer> returnSet = new HashSet<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(file))){
            String line = reader.readLine();
            while (line != null && !line.trim().equals("")) {
                String[] tokens = line.split("\\s*,\\s*");
                //不同
                Customer customer = new Customer(Integer.parseInt(tokens[0]), tokens[1], tokens[2], tokens[3]);
                returnSet.add(customer);
                line = reader.readLine();
            }
        }
        return returnSet;
    }
}

对于本例来说,仅仅有两个实体,可是一个真正的系统可能有几十个实体,所以有非常多反复易错的代码。

你可能会发现Dao层有着同样的情况。在每个Dao进行增删改查的时候差点儿都是同样的操作。唯一与不同的是实体和表。让我们重构这些烦人的代码吧。依据GoF设计模式第一部分提到的原则之中的一个,我们应该“封装不同的概念“ProductCsvReader和CustomerCsvReader之间,不同的是有凝视的代码。所以我们要做的是。把同样的放到一个类。不同的抽取到还有一个类。我们先開始编写ProductCsvReader,我们使用Extract Method提取带凝视的部分:

ProductCsvReader.java after Extract Method

public class ProductCsvReader {

    Set<Product> getAll(File file) throws IOException {
Set<Product> returnSet = new HashSet<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))){
String line = reader.readLine();
while (line != null && !line.trim().equals("")) {
String[] tokens = line.split("\\s*,\\s*");
Product product = unmarshall(tokens);
returnSet.add(product);
line = reader.readLine();
}
}
return returnSet;
} Product unmarshall(String[] tokens) {
Product product = new Product(Integer.parseInt(tokens[0]), tokens[1],
new BigDecimal(tokens[2]));
return product;
}
}

如今我们已经把同样(反复)的代码和不同(各自特有)的代码分开了,我们要创建一个父类AbstractCsvReader,它包括两个类(ProductReader和CustomerReader)同样的部分。我们把它定义为一个抽象类。由于我们不须要实例化它。然后我们将使用Pull Up Method重构这个父类。

AbstractCsvReader.java

abstract class AbstractCsvReader {

    Set<Product> getAll(File file) throws IOException {
Set<Product> returnSet = new HashSet<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))){
String line = reader.readLine();
while (line != null && !line.trim().equals("")) {
String[] tokens = line.split("\\s*,\\s*");
Product product = unmarshall(tokens);
returnSet.add(product);
line = reader.readLine();
}
}
return returnSet;
}
}

ProductCsvReader.java after Pull Up Method

public class ProductCsvReader extends AbstractCsvReader {

    Product unmarshall(String[] tokens) {
Product product = new Product(Integer.parseInt(tokens[0]), tokens[1],
new BigDecimal(tokens[2]));
return product;
}
}

假设在子类中没有‘unmarshall’方法,该类就无法进行编译(它调用unmarshall方法),所以我们要创建一个叫unmarshall的抽象方法。

AbstractCsvReader.java with abstract unmarshall method

abstract class AbstractCsvReader {

    Set<Product> getAll(File file) throws IOException {
Set<Product> returnSet = new HashSet<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))){
String line = reader.readLine();
while (line != null && !line.trim().equals("")) {
String[] tokens = line.split("\\s*,\\s*");
Product product = unmarshall(tokens);
returnSet.add(product);
line = reader.readLine();
}
}
return returnSet;
} abstract Product unmarshall(String[] tokens);
}

如今。在这一点上,AbstractCsvReader是ProductCsvReader的父类,但不是CustomerCsvReader的父类。假设CustomerCsvReader继承AbstractCsvReader编译会报错。为了解决问题我们使用泛型。

AbstractCsvReader.java with Generics

abstract class AbstractCsvReader<T> {

    Set<T> getAll(File file) throws IOException {
Set<T> returnSet = new HashSet<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))){
String line = reader.readLine();
while (line != null && !line.trim().equals("")) {
String[] tokens = line.split("\\s*,\\s*");
T element = unmarshall(tokens);
returnSet.add(product);
line = reader.readLine();
}
}
return returnSet;
} abstract T unmarshall(String[] tokens);
}

ProductCsvReader.java with Generics

public class ProductCsvReader extends AbstractCsvReader<Product> {

    @Override
Product unmarshall(String[] tokens) {
Product product = new Product(Integer.parseInt(tokens[0]), tokens[1],
new BigDecimal(tokens[2]));
return product;
}
}

CustomerCsvReader.java with Generics

public class CustomerCsvReader extends AbstractCsvReader<Customer> {

    @Override
Customer unmarshall(String[] tokens) {
Customer customer = new Customer(Integer.parseInt(tokens[0]), tokens[1],
tokens[2], tokens[3]);
return customer;
}
}

这就是我们要的!

不再有反复的代码。父类中的方法是“模板”,它包括这不变的代码。那些变化的东西作为抽象方法。在子类中实现。记住,当你重构的时候,你应该有自己主动化的单元測试来保证你不会破坏你的代码。

我使用JUnit,你能够使用我帖在这里的代码,也能够在这个Github库找一些其它设计模式的样例。在结束之前,我想说一下模板方法的缺点。模板方法依赖于继承。患有 the Fragile Base Class Problem。简单的说就是,改动父类会对继承它的子类造成意想不到的不良影响。其实,基础设计原则之中的一个的GoF设计模式提倡“多用组合少用继承”。而且更多设计模式也告诉你怎样避免代码反复,同一时候又让复杂或easy出错的代码尽量少的依赖继承。欢迎交流,以便我能够提高我的博客质量。

原文地址。Template Method Pattern Example Using Java Generics

翻译的不好。欢迎拍砖。


新秀翻译(两)——使用Java通用配置模板方法模式的更多相关文章

  1. 折腾Java设计模式之模板方法模式

    博客原文地址:折腾Java设计模式之模板方法模式 模板方法模式 Define the skeleton of an algorithm in an operation, deferring some ...

  2. Java设计模式之模板方法模式(Template Method)

    一.含义 定义一个算法中的操作框架,而将一些步骤延迟到子类中.使得子类可以不改变算法的结构即可重定义该算法的某些特定步骤,不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不同的实现. 二 ...

  3. Java抽象类应用—模板方法模式

    模板方法模式(Templete method) 定义一个操作中的算法的骨架,而将一些可变部分的实现延迟到子类中,模板方法模式使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定的步骤. 例: ...

  4. Java设计模式应用——模板方法模式

    所谓模板方法模式,就是在一组方法结构一致,只有部分逻辑不一样时,使用抽象类制作一个逻辑模板,具体是实现类仅仅实现特殊逻辑就行了.类似科举制度八股文,文章结构相同,仅仅具体语句有差异,我们只需要按照八股 ...

  5. Java设计模式之模板方法模式(Template)

    前言: 我们在开发中有很多固定的流程,这些流程有很多步凑是固定的,比如JDBC中获取连接,关闭连接这些流程是固定不变的,变动的只有设置参数,解析结果集这些是根据不同的实体对象“来做调整”,针对这种拥有 ...

  6. 从西天取经的九九八十一难来看Java设计模式:模板方法模式

    目录 示例 模板方法模式 定义 意图 主要解决问题 适用场景 优缺点 西天取经的九九八十一难 示例 当我们设计一个类时,我们能明确它对外提供的某个方法的内部执行步骤, 但一些步骤,不同的子类有不同的行 ...

  7. Java/C++实现模板方法模式---数据库操作

    对数据库的操作一般包括连接.打开.使用.关闭等步骤,在数据库操作模板类中我们定义了connDB().openDB().useDB().closeDB()四个方法分别对应这四个步骤.对于不同类型的数据库 ...

  8. java设计模式之模板方法模式

    模板方法模式 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中. 模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤.通俗的说的就是有很多相同的步骤的,在某一些地方可能有一些差 ...

  9. 菜鸟译文(二)——使用Java泛型构造模板方法模式

    如果你发现你有很多重复的代码,你可能会考虑用模板方法消除容易出错的重复代码.这里有一个例子:下面的两个类,完成了几乎相同的功能: 实例化并初始化一个Reader来读取CSV文件: 读取每一行并解析: ...

随机推荐

  1. Leetcode 3Sum Closet

    二手和3Sum像几乎相同的想法.二进制搜索.关键修剪.但是,在修剪做出很多错误. 然后还有一个更加速了原来的想法O(n^2). #include<iostream> #include &l ...

  2. 【原创】leetCodeOj --- Binary Tree Right Side View 解题报告

    二连水 题目地址: https://leetcode.com/problems/binary-tree-right-side-view/ 题目内容: Given a binary tree, imag ...

  3. JQuery+CSS3实现封装弹出登录框效果

    原文:JQuery+CSS3实现封装弹出登录框效果 上次发了一篇使用Javascript来实现弹出层的效果,这次刚好用了JQuery来实现,所以顺便记录一下: 因为这次使用了Bootstrap来做一个 ...

  4. 获取listboxitem在ListBox中的index并转换成abcd

    原文 获取listboxitem在ListBox中的index并转换成abcd 截图如下: 1.实现Converter  获取到listbox,并得到listitem在listbox中的index p ...

  5. ASP.Net 重写IHttpModule 来拦截 HttpApplication 实现HTML资源压缩和空白过滤

    务实直接上代码: 1. 重写FilterModule.cs using System; using System.Collections.Generic; using System.Linq; usi ...

  6. MQTT学习笔记——Yeelink MQTT维修 采用mqtt.js和paho-mqtt

    0 前言     2014年8月yeelink推出基于MQTT协议的开关类型设备控制API.相比于基于HTTP RESTful的轮训方式,通过订阅相关主题消息,能够远程控制类应用实时性更好. 本文使用 ...

  7. centos7 设备 mariadb-10

    下载地址: http://mirrors.ustc.edu.cn/mariadb/mariadb-10.0.19/source/mariadb-10.0.19.tar.gz 由于用cmake所以线安装 ...

  8. 给Ubuntu安装KDE桌面 [转]

    自ubuntu5.10发布以来,我非常难得的让她在我的硬盘上顽强生存了近3个月,仔细想想就连当年我第一个接触的红旗Linux也不过坚持了一个月,呵呵,又写了一段废话── 所 谓的“重回Kununtu” ...

  9. [TWRP 2.8.4] for 小米2S/2SC 支持中英文切换

    其中这个 twrp 2.8.4 在18号的下午已经编译好了. 经历了2个小时的代码修改,再经过后期的调试,中英文双语版本的twrp出炉了. 下面上几张图: 图片1:为英文界面 图片2: 图片3:中文界 ...

  10. LinearLayout具体解释一:LinearLayout的简单介绍

    LinearLayout,中文意思是线性布局.假设你是初学android的,肯定会非常困惑"啥叫布局",啥又叫"线性布局"呢. 有的时候,我尝试用官方的语言去解 ...