借贷模式

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. mysql5.6更改datadir数据存储目录

    环境需求: 有些数据存储场景可能需要将数据放到指定的挂载路径或目录,mysql默认存放数据路径在:/var/lib/mysql下. 测试环境: 操作步骤: 1. 查看当前存储目录 [root@mysq ...

  2. PHP函数memory_get_usage获取PHP内存清耗量

    (PHP 4 >= 4.3.2, PHP 5, PHP 7) memory_get_usage — 返回分配给 PHP 的内存量 说明 int memory_get_usage ([ bool ...

  3. (一)flask-sqlalchemy的安装和配置

    在使用flask-sqlalchemy之前要先了解ORM模型,什么叫做ORM模型 一.什么是ORM ORM 全拼Object-Relation Mapping. 称为对象-关系映射 主要实现模型对象到 ...

  4. 编写高质量代码:改善Java程序的151个建议 --[36~51]

    编写高质量代码:改善Java程序的151个建议 --[36~51] 工具类不可实例化 工具类的方法和属性都是静态的,不需要生成实例即可访 问,而且JDK也做了很好的处理,由于不希望被初始化,于是就设置 ...

  5. Spring -- 自定义转换器

    Spring 定义了 3 种类型的转换器接口,实现任意一个转换器接口都可以作为自定义转换器注册到 ConversionServiceFactoryBean 中: Converter<S,T> ...

  6. BZOJ 1912: [Apio2010]patrol 巡逻 (树的直径)(详解)

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1912 题解: 首先,显然当不加边的时候,遍历一棵树每条边都要经过两次.那么现在考虑k==1 ...

  7. Callable和Future、FutureTask的使用

    http://www.silencedut.com/2016/06/15/Callable%E5%92%8CFuture%E3%80%81FutureTask%E7%9A%84%E4%BD%BF%E7 ...

  8. poj2054 Color a Tree

    神题.这题是巨毒瘤... 自己写真可谓是: 排空驭气奔如电,上天入地求之遍 上穷碧落下黄泉,两处茫茫皆不见 由于我们知道:不是树形时,不停选值最大的节点可以得到最小代价. 那么我们就能想出一个错误的贪 ...

  9. poj2559 Largest Rectangle in a Histogram

    洛谷上做过一道一样的题(P1719 最大加权矩形),但是没写博客... 现在已一个新高度来看待这题,沿用以前的方法,感觉很好(草稿纸模拟数小时后20分钟AC) 就是对于每一个位置,记录能够往右延伸多远 ...

  10. CentOS7利用systemctl添加自定义系统服务

    CentOS7的每一个服务以.service结尾,一般会分为3部分:[Unit].[Service]和[Install] 转载于互联网 [Unit] 部分主要是对这个服务的说明,内容包括Descrip ...