让我们来看一个简单的例子,我们如何在Groovy中实现DSL -
class EmailDsl { String toText String fromText String body /** * This method accepts a closure which is essentially the DSL. Delegate the * closure methods to * the DSL class so the calls can be processed */ def static make(closure) { EmailDsl emailDsl = new EmailDsl() // any method called in closure will be delegated to the EmailDsl class closure.delegate = emailDsl closure() } /** * Store the parameter as a variable and use it later to output a memo */ def to(String toText) { this.toText = toText } def from(String fromText) { this.fromText = fromText } def body(String bodyText) { this.body = bodyText } } EmailDsl.make { to "Nirav Assar" from "Barack Obama" body "How are things? We are doing well. Take care" }
当我们运行上面的程序,我们将得到以下结果 -
How are things? We are doing well. Take care
以下需要注意上面的代码实现 -
使用接受闭包的静态方法。这是一个很麻烦的方式来实现DSL。
在电子邮件示例中,类EmailDsl具有make方法。它创建一个实例,并将闭包中的所有调用委派给实例。这是一种机制,其中“to”和“from”节结束了EmailDsl类中的执行方法。
一旦to()方法被调用,我们将文本存储在实例中以便以后格式化。
我们现在可以使用易于为最终用户理解的简单语言调用EmailDSL方法。
标签:Groovy
相关阅读 >>
更多相关阅读请进入《Groovy》频道 >>

深入理解Java虚拟机 JVM高级特性与实践 周志明 第3版
这是一部从工作原理和工程实践两个维度深入剖析JVM的著作,是计算机领域公认的经典。