scala 借贷模式(loan pattern)通用实现
来源:互联网 发布:怎么检查网络是否连接 编辑:程序博客网 时间:2024/05/16 19:57
import scala.language.reflectiveCallsimport util.control.Exception._import scala.concurrent.{ ExecutionContext, Future }object LoanPattern extends LoanPattern/** * Loan pattern implementation */trait LoanPattern { type Closable = { def close() } def using[R <: Closable, A](resource: R)(f: R => A): A = { try { f(resource) } finally { ignoring(classOf[Throwable]) apply { resource.close() } } } /** * Guarantees a Closeable resource will be closed after being passed to a block that takes * the resource as a parameter and returns a Future. */ def futureUsing[R <: Closable, A](resource: R)(f: R => Future[A])(implicit ec: ExecutionContext): Future[A] = { f(resource) andThen { case _ => resource.close() } // close no matter what }}
调用:
LoanPattern.using(resource)(f)比如resource可以是jdbc connection或者是file等等资源,在使用完之后,会自动调用close方法关闭资源。
0 0
- scala 借贷模式(loan pattern)通用实现
- 理解贷出模式 Loan Pattern
- scala(2)借贷模式
- Loan Pattern
- 借贷模式
- 借贷模式
- [Scala函数特性系列]——作为值的函数和借贷模式
- 观察者模式之Scala实现
- scala实现观察者模式示例
- 观察者模式与scala实现
- 借贷
- 代理模式通用模板实现
- scala实现设计模式之原型模式
- scala实现设计模式之适配器模式
- scala实现设计模式之组合模式
- scala实现设计模式之外观模式
- scala实现设计模式之代理模式
- scala实现设计模式之命令模式
- uc/os-iii学习笔记-任务调度
- CodeForces 447C DZY Loves Sequences (dp 子序列)
- hdu3849 Tarjan求无向图的割边+map
- "应用程序无法正常启动(0xc000007b)。请单击 确定 关闭应用程序。"解决方法
- 虚拟机中LINUX环境的oracle中文乱码的问题
- scala 借贷模式(loan pattern)通用实现
- 模块化的JavaScript开发的优势在哪里
- 关于算法的一点思考。。。
- .NET CORE实践(1)--Ubuntu下的Hello World
- STM32 FSMC控制器
- Linux 命令行搭建Java环境
- 使用汇编语言编程常见错误与技巧
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- oracle常用命令