Groovy 设计模式 -- 借贷
借贷模式
http://groovy-lang.org/design-patterns.html#_loan_my_resource_pattern
The Loan my Resource pattern ensures that a resource is deterministically disposed of once it goes out of scope.
This pattern is built in to many Groovy helper methods. You should consider using it yourself if you need to work with resources in ways beyond what Groovy supports.
模式反例
def reader = f.newReader()
reader.splitEachLine(' ') { wordList ->
println wordList
}
reader.close()
// =>
// [ "Mon", "Jun", "18", "22:38:17", "EST", "2007" ]
// [ "RunPattern" ]
模式正例
def withListOfWordsForEachLine(File f, Closure c) {
def r = f.newReader()
try {
r.splitEachLine(' ', c)
} finally {
r?.close()
}
}Now, we can re-write our code as follows:
withListOfWordsForEachLine(f) { wordList ->
println wordList
}
// =>
// [ "Mon", "Jun", "18", "22:38:17", "EST", "2007" ]
// [ "RunPattern" ]
This is much simpler and has removed the explicit close(). This is now catered for in one spot so we can apply the appropriate level of testing or reviewing in just one spot to be sure we have no problems.
Groovy 设计模式 -- 借贷的更多相关文章
- Groovy 设计模式 -- 迭代器模式
Iterator Pattern http://groovy-lang.org/design-patterns.html#_flyweight_pattern 迭代器模式,允许顺序访问 聚集对象中的中 ...
- Groovy 设计模式 -- 保镖模式
Bouncer Pattern http://groovy-lang.org/design-patterns.html#_bouncer_pattern 保镖模式主要负责对函数的输入参数的合法性检查, ...
- Groovy 设计模式 -- 享元模式
Flyweight Pattern 享元模式, 将对象的相同属性, 以节省内存为目的,存储为一份公共对象, 所有对象共用此分对象. The Flyweight Pattern is a pattern ...
- Groovy 设计模式 -- 装饰器模式
http://groovy-lang.org/design-patterns.html#_chain_of_responsibility_pattern 装饰器模式, 起到美化原始对象的作用. 一个被 ...
- Groovy 设计模式 -- 组合模式
Composite Pattern http://groovy-lang.org/design-patterns.html#_chain_of_responsibility_pattern 组合模式, ...
- Groovy 设计模式 -- 责任链模式
Chain of Responsibility Pattern http://groovy-lang.org/design-patterns.html#_chain_of_responsibility ...
- Groovy 设计模式 -- 适配器模式
Adapter Pattern http://groovy-lang.org/design-patterns.html#_adapter_pattern 适配器模式,对象存在一个接口, 此接口在此对象 ...
- Groovy 设计模式 -- null对象模式
Null Object Pattern http://groovy-lang.org/design-patterns.html#_loan_my_resource_pattern 对于一些场景获得的对 ...
- Groovy 设计模式 -- 抽象工厂 模式
抽象工厂 https://blog.csdn.net/wyxhd2008/article/details/5597975 首先来看看这两者的定义区别: 工厂模式:定义一个用于创建对象的借口,让子类决定 ...
随机推荐
- 「SCOI2016」妖怪 解题报告
「SCOI2016」妖怪 玄妙...盲猜一个结论,然后过了,事后一证,然后假了,数据真水 首先要最小化 \[ \max_{i=1}^n (1+k)x_i+(1+\frac{1}{k})y_i \] \ ...
- centos7安装mha4mysql
mysql搭建mha需要用的两个rpm包.(manager包和node包) 下载地址:https://download.csdn.net/download/dajdajdajdaj/10603389 ...
- 编写高质量代码:改善Java程序的151个建议 --[106~117]
编写高质量代码:改善Java程序的151个建议 --[106~117] 动态代理可以使代理模式更加灵活 interface Subject { // 定义一个方法 public void reques ...
- 解决plink报错:.bim file has a split chromosome. Use --make-bed by itself to remedy this.
由于plink1.9和1.07这两个版本互掐,经常出现各种不兼容问题,“.bim file has a split chromosome. Use --make-bed by itself to r ...
- WebService 及 CXF 的进阶讲解
4.2. WebService请求深入分析 1). 分析WebService的WSDL文档结构 1.1). 实例截图 <definitions> <types> <sch ...
- poj 3273"Monthly Expense"(二分搜索+最小化最大值)
传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...
- Gym - 101755G Underpalindromity (树状数组)
Let us call underpalindromity of array b of length k the minimal number of times one need to increme ...
- Linux下进程与线程的区别及查询方法
在平时工作中,经常会听到应用程序的进程和线程的概念,那么它们两个之间究竟有什么关系或不同呢?一.深入理解进程和线程的区别 1)两者概念 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进 ...
- Inception介绍(MySQL自动化运维工具)
Inception介绍 GitHub:https://github.com/mysql-inception/inception 文档:https://mysql-inception.github.io ...
- 访问内网(https,udp)
安装teamview 客户端. 安装vpn驱动(这里的VPN应该是A主机与B客户端分别连接上了teamview的服务器,但是本身不是局域 网,所以不能直接访问A的局域网的其他主机) 主机端A,安装主机 ...