Groovy A simple DSL based on groovy

import groovy.transform.Field;
 
@Field def eat = new Expando()
 
class FriendHadnler {
    def scriptObject
 
    void friend(String name) {
        scriptObject.eat.friend = name
    }
}
 
@Field def friendHadnler = new FriendHadnler(scriptObject : this)
 
void place(String place) {
    eat.place = place
}
 
void time(String time) {
    eat.time = time
}
 
void withFriend(Closure cl) {
    cl.setResolveStrategy(Closure.DELEGATE_FIRST)
    cl.setDelegate(friendHadnler)
    cl.call()
}
 
def eatSth(Closure cl) {
    cl.call()
    println eat.place + ' ' + eat.time + ' with friend ' + eat.friend
}
 
eatSth {
    place     'meituan tower'
    time      '12:30 pm'
    withFriend {
        friend 'han meimei'
    }
}

  

posted @ 2015-10-15 11:47  lotusJade  阅读(177)  评论(0编辑  收藏  举报