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 首先来看看这两者的定义区别: 工厂模式:定义一个用于创建对象的借口,让子类决定 ...
随机推荐
- 每天一个Linux命令(03):du命令
du命令 今天找开发定位问题,看到他使用了这个命令,查看文件,之前知道df,所以今天的每天系列把这命令 du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空 ...
- centos7搭建ELK Cluster集群日志分析平台(一):Elasticsearch
应用场景: ELK实际上是三个工具的集合,ElasticSearch + Logstash + Kibana,这三个工具组合形成了一套实用.易用的监控架构, 很多公司利用它来搭建可视化的海量日志分析平 ...
- hdu2795(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一个h*w的公告牌,要在其上贴公告.现在有n个公告,每个公告的尺寸为1*wi,即高度 ...
- 软件在 win7 上运行时显示乱码
一个用户反应后,我当时就蒙圈了,因为之前从未遇到过: 百度一下后,发现用户的这种情况比较特殊,从表面上看,[控制面板]和[注册表]相关项设置都正常,为什么还显示乱码呢? 到最后一步如果已经是(简体,中 ...
- 在Derby中取得刚刚插入的“递增”类型的字段值
现在才发现采用不同的数据库,对写程序影响很大. 以前常用SQL Server2000或Access,可能是因为都是Microsoft公司的产品,所以在从不同的平台转换的时候问题不是很大. 现在采用De ...
- react-native中timer的注意点
务必在卸载组件前清除定时器! 我们发现很多 React Native 应用发生致命错误(闪退)是与计时器有关.具体来说,是在某个组件被卸载(unmount)之后,计时器却仍然在运行.要解决这个问题,只 ...
- C connect实现Timeout效果(Linux)
C connect函数是阻塞的,现要实现非阻塞式的connect. int SocketClient::connectTimeOut(const int &connect_fd, const ...
- Windows 查看端口占用情况
今天打算运行一下当年的毕业设计,结果启动ActiveMQ的时候,发现报错 原来是端口占用了.在Windows上怎样看呢? Ctrl+Alt+Del 调出任务管理器 再找到资源监视器 原来是依赖于Erl ...
- SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)
先建立父工程 .. ..一路next 搭建注册中心(需要建立三个工程,端口不一样) .. .. .. 修改入口类 package com.cloud.eurekaserver1111; import ...
- Solr7.1--- 指定ConfigSets
我们都知道Solr提供了一个默认的配置,以及一个demo 但是实际场景是我们需要自己独立配置,不用默认的,主要为了方便管理. 进入目录: D:\solr-7.1.0\server\solr\confi ...