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

模式定义

模板方法: 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中,模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。

注意:工厂方法及抽象工厂方法着重抽象的是产品,而模板方法着重抽象的是步骤。

需求背景

在实际开发过程中,经常遇到各种格式的数据之间的转换,如 json转java bean, XML转java bean,YAML转java bean, 这个时候模板模式是很好的选择。

具体实现

  1. 生成转换工具类模板
public abstract class Formatter {  
  
    public String formatBook(Book book, int format) {  
        beforeFormat();  
        String result = formating(book);  
        afterFormat();  
        return result;  
    }  
  
    protected void beforeFormat() {  
        System.out.println("format begins");  
    }  
  
    protected abstract String formating(Book book);  
  
    protected void afterFormat() {  
        System.out.println("format finished");  
    }  
  
}  

你会发现format_book方法只有四步,第一步调用before_format,去打印格式转换前的日志。第二步调用formating,这个方法是个抽象方法,用于处理具体的转换逻辑,因此每一个继承自Formatter的子类都需要重写此方法,来实现各自的转换逻辑。第三步调用after_format,去打印格式转换后的日志。第四步返回result。由于类中存在了抽象方法,我们也就需要把Formatter声明成抽象类。

然后要定义专门的子类来处理每种传输格式的具体逻辑,这样不同传输格式的逻辑可以从一个方法里分离开,明显便于阅读和理解。

  1. XMLFormatter 继承Formatter 生成具体的转换工具
public class XMLFormatter extends Formatter {  
  
    @Override  
    protected String formating(Book book) {  
        String result = "";  
        result += "<book_name>" + book.getBookName() + "</book_name>\n";  
        result += "<pages>" + book.getPages() + "</pages>\n";  
        result += "<price>" + book.getPrice() + "</price>\n";  
        result += "<author>" + book.getAuthor() + "</author>\n";  
        result += "<isbn>" + book.getIsbn() + "</isbn>\n";  
        return result;  
    }  
  
}  
  1. JSONFormatter继承Formatter生成具体的转换工具
public class JSONFormatter extends Formatter {  
  
    @Override  
    protected String formating(Book book) {  
        String result = "";  
        result += "{\n";  
        result += "\"book_name\" : \"" + book.getBookName() + "\",\n";  
        result += "\"pages\" : \"" + book.getPages() + "\",\n";  
        result += "\"price\" : \"" + book.getPrice() + "\",\n";  
        result += "\"author\" : \"" + book.getAuthor() + "\",\n";  
        result += "\"isbn\" : \"" + book.getIsbn() + "\",\n";  
        result += "}";  
        return result;  
    }  
  
}  
  1. YAMLFormatter继承Formatter生成具体的转换工具
public class YAMLFormatter extends Formatter {  
  
    @Override  
    protected String formating(Book book) {  
        String result = "";  
        result += "book_name: " + book.getBookName() + "\n";  
        result += "pages: " + book.getPages() + "\n";  
        result += "price: " + book.getPrice() + "\n";  
        result += "author: " + book.getAuthor() + "\n";  
        result += "isbn: " + book.getIsbn() + "\n";  
        return result;  
    }  
  
}  
  1. 测试
public class Test {  
  
    public static void main(String[] args) throws Exception {  
        Book book = new Book();  
        book.setBookName("Thinking in Java");  
        book.setPages(880);  
        book.setPrice(68);  
        book.setAuthor("Bruce Eckel");  
        book.setIsbn("9787111213826");  
        XMLFormatter xmlFormatter = new XMLFormatter();  
        String result = xmlFormatter.formatBook(book);  
        System.out.println(result);  
        JSONFormatter jsonFormatter = new JSONFormatter();  
        result = jsonFormatter.formatBook(book);  
        System.out.println(result);  
        YAMLFormatter yamlFormatter = new YAMLFormatter();  
		String result = yamlFormatter.formatBook(book);  
		System.out.println(result);
    }  
  
}  

模板模式参考链接

参考链接:
https://blog.csdn.net/cdl2008sky/article/details/3335385
http://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Calvin880828

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值