业余时间学了学Scala,至今有3个多月了...这两天看到群(54813143)里有同学讨论AOP,于是就想出了如下实现;
见代码及注释,欢迎喜欢Scala的同学一起讨论:
//trait里没有任何抽象的值或方法
trait AOP{
private var targets=List[Function1[String,Any]]() //List里放的都是函数对象
private val placeholder="" //无意义,用来调用函数对象的
def before=println("before...")
def after=println("after...")
def invoke(target:Function1[String,Any])={ //参数是个函数对象
before
target(placeholder) //什么时候调用,自己放置顺序
after
}
def add(t:Function1[String,Any])= targets=t::targets //收集所有被拦截的方法(函数对象)
def invokeAll()={
before
targets.reverse.foreach(t=>t(placeholder)) //reverse:保证调用的顺序(FIFO)
after
//这里需要一个清空targets的方法,就不写了
}
}
class Write extends AOP{
def writeOne(x:String,y:String)(placeholder:String)={ //curry化,最后一个placeholder算是冗余参数
println("one working..."+x+y)
}
def writeTwo(x:String,y:String,z:String)(placeholder:String)={
println("two working..."+x+y+z)
}
}
val w=new Write()
w.invoke(w.writeOne("hello"," world")_) //拦截的方式,使用了偏应用函数
w.add(w.writeOne("hello"," world")_) //收集
w.add(w.writeTwo("hello"," world"," more")_) //再收集
w.invokeAll //统一拦截
这里只是给喜欢Scala的同学一个思路,期望在此思路上进行扩展。
是不是很优雅? 还在等什么,快快加入到Scala的学习队伍中来吧。
分享到:
相关推荐
CrossCuttingConcern_Scala 在 Scala 中使用面向切面编程 (AOP) 实现横切关注
glicko2-scala Scala中实现的Glicko2排名系统
Scala:Applied Machine Learning by Pascal Bugnion English | 23 Feb. 2017 | ISBN-13: 9781787126640 | 1843 Pages | EPUB/PDF (conv) | 33.15 MB Leverage the power of Scala and master the art of building, ...
将org.plotly-scala::plotly-almond:0.8.1依赖项添加到笔记本中。 (最新版本:)然后初始化plotly-scala,并像使用它一样使用它 import $ivy . `org.plotly-scala::plotly-almond:0.8.1` import plotly . _ import
"Scala: Guide for Data Science Professionals (Learning Path)" ASIN: B06XCJVY21, eISBN: 1787282856 | 2017 | True PDF | 1100 pages | 15 MB Scala will be a valuable tool to have on hand during your data...
ddd-on-scala:Scala的DDD示例实现
hello-grpc-scala:使用ScalaPB的gRPC简介
scala:scala代码
本书是Scala编程语言的权威教程,由该语言的设计师Martin Odersky共同编写。
design-patterns-scala:用Scala语言实现设计模式
curryhoward:通过Curry-Howard同构为Scala函数和表达式自动生成代码
Ataraxer 国际象棋:Scala描述用Scala编程语言简单实现国际象棋游戏。
广告流量实时统计(Scala)
Programming Scala: Tackle Multi-Core Complexity on the Java Virtual Machine by Venkat Subramaniam Scala is an exciting, modern, multi-paradigm language for the JVM. You can use it to write ...
用Scala编程语言实现各种算法 如何使用此存储库进行学习? 在本地计算机上克隆存储库 git clone https://github.com/TheAlgorithms/Scala.git 使用IDE(最好是IntelliJ)打开存储库 启用自动导入以从build.sbt...
Testcontainers-scala 用于Scala包装器,允许使用容器进行功能/... 行动中的Testcontainers-scala: ://dimafeng.com/2016/08/01/testcontainers-selenium/松弛设置对于scalatest用户: libraryDependencies + = " ...
将使用编译为 JavaScript 的文件导入您的应用程序。 安装 ember install:addon ember-cli-scala 用法 app/scala/Robot.scala : package robot import scala . scalajs . js import js . annotation . JSExport @...
vertx-lang-scalaCI结构vertx-lang-scala:包含实际语言实现的主模块vertx-lang-scala-stack:所有当前可用的模块vertx-lang-scala-on-the-fly:一种编译器实现,允许从源/类路径编译Scala代码。
C.scala:C 的浅层 DSL 该项目为核心 C 语言定义了一个 Scala 库和一些有用的 C 库。 它还提供了 Scala 集合的实现,例如 C DSL 中的 HashMap 或 List。 安装 浅层 C 定义伴随着使用 JNI 的实现。 sbt compile将...
赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 包含翻译后的API...