借贷模式

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 设计模式 -- 借贷的更多相关文章

  1. Groovy 设计模式 -- 迭代器模式

    Iterator Pattern http://groovy-lang.org/design-patterns.html#_flyweight_pattern 迭代器模式,允许顺序访问 聚集对象中的中 ...

  2. Groovy 设计模式 -- 保镖模式

    Bouncer Pattern http://groovy-lang.org/design-patterns.html#_bouncer_pattern 保镖模式主要负责对函数的输入参数的合法性检查, ...

  3. Groovy 设计模式 -- 享元模式

    Flyweight Pattern 享元模式, 将对象的相同属性, 以节省内存为目的,存储为一份公共对象, 所有对象共用此分对象. The Flyweight Pattern is a pattern ...

  4. Groovy 设计模式 -- 装饰器模式

    http://groovy-lang.org/design-patterns.html#_chain_of_responsibility_pattern 装饰器模式, 起到美化原始对象的作用. 一个被 ...

  5. Groovy 设计模式 -- 组合模式

    Composite Pattern http://groovy-lang.org/design-patterns.html#_chain_of_responsibility_pattern 组合模式, ...

  6. Groovy 设计模式 -- 责任链模式

    Chain of Responsibility Pattern http://groovy-lang.org/design-patterns.html#_chain_of_responsibility ...

  7. Groovy 设计模式 -- 适配器模式

    Adapter Pattern http://groovy-lang.org/design-patterns.html#_adapter_pattern 适配器模式,对象存在一个接口, 此接口在此对象 ...

  8. Groovy 设计模式 -- null对象模式

    Null Object Pattern http://groovy-lang.org/design-patterns.html#_loan_my_resource_pattern 对于一些场景获得的对 ...

  9. Groovy 设计模式 -- 抽象工厂 模式

    抽象工厂 https://blog.csdn.net/wyxhd2008/article/details/5597975 首先来看看这两者的定义区别: 工厂模式:定义一个用于创建对象的借口,让子类决定 ...

随机推荐

  1. Scout YYF I POJ - 3744(概率dp)

    Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into th ...

  2. centos7安装mysql5.6(rpm包安装)

    应用场景:日常需求安装数据库——MySQL. 安装环境:① 最小化安装的centos7.2(同版本RHEL7适用): ② MySQL 5.6: 具体步骤: 1. 下载mysql 5.6并解压(官网有时 ...

  3. Shiro中的Remember me设置

    1. 在Spring的相关配置文件中加入如下Remember me管理器配置: <!-- rememberMe管理器 --> <bean id="rememberMeMan ...

  4. Typescript学习笔记(三)变量声明及作用域

    ts的变量声明有var,let和const,这尼玛完全跟es6一样嘛.就稍微介绍一下. 大多数js开发者对于var很熟悉了,原生js里没有块级作用域,只有函数作用域和全局作用域,还存在var的变量提升 ...

  5. Redis需要多少内存预留-内存占用多少才安全

    转: Redis需要多少内存预留-内存占用多少才安全 2018年02月10日 18:13:37 常城 阅读数:10280   版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...

  6. 异步ztree 加复选框 及相应后台处理

    异步加载 tree,点一下节点,就发一下请求到后台,然后显示出得到的当前层级节点 <!DOCTYPE html> <html> <head> <meta ch ...

  7. Day27--Python--初识socket

    一. C\S架构, 客户端服务端架构 客户端(client): 享受服务端提供的服务 服务端(server): 给客户端提供服务 B\S 浏览器和服务端 B (browser) 二. 网络通信的整个流 ...

  8. 计算机基础:计算机网络-chapter2

    因为计算机网络是的传输从物理到数据端的过程到端是自下而上的,所以大多数的教材都是从五层协议结构的底部开始向上延申. 物理层: = = 好复杂哦.看不懂,反正就是讲一些通信的线,信号传输的实现原理,和信 ...

  9. python自动化开发-[第十七天]-django的ORM与其他

    今日概要: 1.name别名 2.模版的深度查询 3.模版语言之filter 4.自定义过滤器,filter和simpletag的区别 5.orm进阶 扫盲:url的组成 URL:协议+域名+端口+路 ...

  10. python自动化开发-[第八天]-面向对象高级篇与网络编程

    今日概要: 一.面向对象进阶 1.isinstance(obj,cls)和issubclass(sub,super) 2.__setattr__,__getattr__,__delattr__ 3.二 ...