<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    First they ignore you
    then they ridicule you
    then they fight you
    then you win
        -- Mahatma Gandhi
    Chinese => English     英文 => 中文             
    隨筆-221  評論-1047  文章-0  trackbacks-0
    利用Groovy與生俱來的動態(tài)特性,創(chuàng)建DSL(Domain Specific Language)是一件十分容易的事情。
    下面通過一個例子,向大家展示一下用Groovy創(chuàng)建DSL的優(yōu)雅之處:

    利用下面這種語法
    ??? person {
    ??????? name {
    ??????????? firstname 'Daniel'
    ??????????? lastname? 'Sun'
    ??????? }
    ??? }

    ??? person {
    ??????? name {
    ??????????? firstname = '山風(fēng)'
    ??????????? lastname? = '小子'
    ??????? }
    ??? }
    創(chuàng)建一個Person對象。

    def?createMetaClass(Class?clazz,?Closure?closure)?{
    ????
    /*?
    ????????為傳入的Class對象創(chuàng)建一個ExpandoMetaClass實例,但不將該ExpandoMetaClass實例注冊到MetaClassRegistry對象中
    ????
    */
    ????def?emc?
    =?new?ExpandoMetaClass(clazz,?false)?
    ????
    /*
    ????????該closure用來初始化ExpandoMetaClass對象,這種寫法的思想與Template?Method?Pattern有異曲同工之妙
    ????
    */
    ????closure(emc)?
    ????emc.initialize()?
    //?完成初始化過程
    ????return?emc
    }

    def?executeScript(dslScriptCode,?rootName,?closure)?{
    ????Script?dslScript?
    =?new?GroovyShell().parse(dslScriptCode)??//?讀取并解析DSL代碼,返回一個Script對象
    ????
    ????dslScript.metaClass?
    =?createMetaClass(dslScript.class)?{?emc?->
    ????
    /*
    ????????動態(tài)新增一個名為"$rootName"的方法,注意"$rootName"的值決定于運行時,比如本例中的值為person
    ????
    */
    ????????emc.
    "$rootName"?=?closure?
    ????}
    ????
    ????
    return?dslScript.run()?//?執(zhí)行DSL代碼
    }

    class?Name?{
    ????String?firstname
    ????String?lastname
    ????String?toString()?{
    ????????
    "$firstname.$lastname"
    ????}
    }

    class?Person?{
    ????Name?name
    ????Person(name)?{
    ????????
    this.name?=?name
    ????}
    ????String?toString()?{
    ????????
    "My?name?is?$name"
    ????}
    }

    /*
    PersonDelegate對象是下面作為參數(shù)傳入‘person方法’的closure的delegate,形象點說,closure就是那對大括號{}以及大括號中的內(nèi)容
    如果您對closure的delegate不太熟悉,可以參考在下的另一篇文章《Groovy解惑——closure中的delegate》(
    http://m.tkk7.com/BlueSUN/archive/2007/12/22/169580.html)
    ????person?{
    ????????
    ????}
    */
    class?PersonDelegate?{
    ????def?person
    ????PersonDelegate(person)?{
    ????????
    this.person?=?person
    ????}
    ????
    /*?
    ????????關(guān)于methodMissing這一特殊方法,請參考在下的另一篇文章《Groovy高效編程——動態(tài)改變對象的能力》(
    http://m.tkk7.com/BlueSUN/archive/2007/07/15/130318.html)
    ????
    */
    ????def?methodMissing(String?name,?Object?args)?{
    ????????
    if?('name'?==?name?&&?args[0]?instanceof?Closure)?{
    ????????????def?nameClosure?
    =?args[0]
    ????????????
    /*?
    ????????????????給nameClosure的delegate賦值,nameClosure就是name旁邊的那個closure即一對大括號{}以及大括號中的內(nèi)容
    ????????????
    */
    ????????????nameClosure.delegate?
    =?new?NameDelegate(person)??
    ????????????nameClosure.resolveStrategy?
    =?Closure.DELEGATE_FIRST?//?指明closure中變量和方法的解析策略,本例選擇DELEGATE_FIRST
    ????????????nameClosure()
    ????????}
    ????}
    ????
    /*?
    ????????關(guān)于propertyMissing這一特殊方法,請參考在下的另一篇文章《Groovy高效編程——動態(tài)改變對象的能力》(
    http://m.tkk7.com/BlueSUN/archive/2007/07/15/130318.html)
    ????
    */
    ????def?propertyMissing(String?name)?{}
    }

    /*
    類似于PersonDelegate,
    NameDelegate對象是下面作為參數(shù)傳入‘name方法’的closure的delegate
    ????????name?{
    ????????????
    ????????}
    */
    class?NameDelegate?{
    ????def?person
    ????NameDelegate(person)?{
    ????????
    this.person?=?person
    ????}
    /*
    ????下面這些getter和setter是為了實現(xiàn)下面這種賦值而寫的:?firstname?=?'山風(fēng)'和lastname??=?'小子'
    ????person?{
    ????????name?{
    ????????????firstname?=?'山風(fēng)'
    ????????????lastname??=?'小子'
    ????????}
    ????}
    */
    ????def?getFirstname()?{
    ????????
    return?person.name.firstname
    ????}
    ????def?setFirstname(String?firstname)?{
    ????????person.name.firstname?
    =?firstname
    ????}
    ????def?getLastname()?{
    ????????
    return?person.name.lastname
    ????}
    ????def?setLastname(String?lastname)?{
    ????????person.name.lastname?
    =?lastname
    ????}
    ????
    ????def?methodMissing(String?name,?Object?args)?{
    ????????
    if?('firstname'?==?name)?{
    ????????????person.name.firstname?
    =?args[0]
    ????????}?
    else?if?('lastname'?==?name)?{
    ????????????person.name.lastname?
    =?args[0]
    ????????}
    ????}
    ????def?propertyMissing(String?name)?{}
    }

    /*
    ????在這篇文章中,演示了兩種賦值方式,各位可以根據(jù)自己的喜好選擇一種,我個人偏好第一種?:)
    */

    //?本例DSL的第一種寫法
    def?dslScriptCode?=?'''
    ????person?{
    ????????name?{
    ????????????firstname?
    'Daniel'
    ????????????lastname??
    'Sun'
    ????????}
    ????}
    '''

    def?scriptClosure?
    =?{?Closure?personClosure?->
    ????def?person?
    =?new?Person(new?Name())
    ????personClosure.delegate?
    =?new?PersonDelegate(person)
    ????personClosure.resolveStrategy?
    =?Closure.DELEGATE_FIRST
    ????personClosure()
    ????
    ????
    return?person
    }
    def?person?
    =?executeScript(dslScriptCode,?'person',?scriptClosure)

    println?person


    //?本例DSL的第二種寫法
    def?dslScriptCode2?=?'''
    ????person?{
    ????????name?{
    ????????????firstname?
    =?'山風(fēng)'
    ????????????lastname??
    =?'小子'
    ????????}
    ????}
    '''

    def?scriptClosure2?
    =?{?Closure?personClosure?->
    ????def?person2?
    =?new?Person(new?Name())
    ????personClosure.delegate?
    =?new?PersonDelegate(person2)
    ????personClosure.resolveStrategy?
    =?Closure.DELEGATE_FIRST
    ????personClosure()
    ????
    ????
    return?person2
    }
    def?person2?
    =?executeScript(dslScriptCode2,?'person',?scriptClosure2)

    println?person2


    運行結(jié)果:
    My?name?is?Daniel.Sun
    My?name?is?山風(fēng).小子

    附:朝花夕拾——Groovy & Grails
    posted on 2008-05-17 00:38 山風(fēng)小子 閱讀(5703) 評論(1)  編輯  收藏 所屬分類: Groovy & Grails
    主站蜘蛛池模板: 精品免费国产一区二区| 亚洲国产精品无码久久98| 亚洲国产日产无码精品| 亚洲人成人网站18禁| sihu国产精品永久免费| 99视频全部免费精品全部四虎| 又粗又大又长又爽免费视频| 亚洲国产天堂久久综合网站| 国产成人精品亚洲| 18禁美女黄网站色大片免费观看| 国产一级高清免费观看| 亚洲综合网美国十次| 女人裸身j部免费视频无遮挡| 2021国内精品久久久久精免费| 亚洲成a人片在线观看日本麻豆| 亚洲精品人成电影网| 男女拍拍拍免费视频网站| 麻豆成人精品国产免费| 久久精品国产亚洲AV大全| 一级日本高清视频免费观看| 最近中文字幕无吗免费高清| 亚洲视频在线播放| 高清永久免费观看| 免费国产成人午夜私人影视| 国产成+人+综合+亚洲专| 免费污视频在线观看| 久久久久亚洲爆乳少妇无| 精品久久久久亚洲| 最近免费中文字幕大全视频| 亚洲成aⅴ人在线观看| 久久综合九色综合97免费下载| 亚洲片一区二区三区| 色窝窝亚洲av网| 在线观看免费毛片| 亚洲卡一卡二卡乱码新区| 亚洲免费在线视频观看| 久久精品国产亚洲AV果冻传媒| 男女拍拍拍免费视频网站| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 日韩精品内射视频免费观看| 亚洲无码高清在线观看|