借贷模式

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. luogu5021 [NOIp2018]赛道修建 (二分答案+dp(贪心?))

    首先二分一下答案,就变成了找长度>=m的 不相交的路径的个数 考虑到在一个子树中,只有一个点能出这个子树去和别的点搞 所以我这个子树里尽量自我满足是不会有坏处的 而且要在自我满足数最大的条件下, ...

  2. BZOJ3566: [SHOI2014]概率充电器 树形+概率dp

    3566: [SHOI2014]概率充电器 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1888  Solved: 857[Submit][Stat ...

  3. 51nod 1005 1027 1029 高精度

    Java大数用法参考:https://www.cnblogs.com/jin-nuo/p/5313205.html 1005 大数加法: import java.util.*; import java ...

  4. Quick Guide to Microservices with Spring Boot 2.0, Eureka and Spring Cloud

    https://piotrminkowski.wordpress.com/2018/04/26/quick-guide-to-microservices-with-spring-boot-2-0-eu ...

  5. Lisp经典算法

    求平方根 SUCCESSIVE AVERAGING DUE TO HERON OF ALEXANDRIA ** TO FIND AN APPROXIMATION TO SQRT(X) ** MAKR ...

  6. 洛谷P1117 优秀的拆分

    题意:求一个字符串中有多少形如AABB的子串. 解:嗯...我首先极度SB的想了一个后缀自动机套线段树启发式合并的做法,想必会TLE. 然后跑去看题解,发现实在是妙不可言... 显然要对每个位置求出向 ...

  7. 爬虫 写入文件时遇到gbk编码错误

    #获取视频地址 # 每次请求一次,然后写文件,这样可以规避多次请求触发反爬虫 r = requests.get('https://www.pearvideo.com/video_1522192') h ...

  8. 关于TSql

    1.Sql:结构化查询语言(Structrued  Query  Language) 2.TSql:是Sql语言的另一种版本,且只能在SqlServer中使用.和Sql不同的是,TSql中增加了对变量 ...

  9. python 中r 和 \r

    r'xxx' 转义 如果在前面加r字符,则表示让这个字符串里面的内容失去转义的意义 1 s = r'\n这只是\n' # 字符串中的"\n"只是字符,没有换行的意义了. 2 pri ...

  10. (DIjsktra算法) nyoj1401-一场说走就走的旅行

    题目描述: 有一天,孩子回来对我说:“妈妈,听说马尔代夫很不错,放假了我想去玩.”马尔代夫?我也想去!没有人不向往一场说走就走的旅行!“其实我想去的地方很多,呼伦贝尔大草原.玉龙雪山.布达拉宫.艾菲尔 ...