定义复杂的模型

1.通过组合定义复杂的模型

    一个XML可以包含多个节点,而每个节点又可以包含多个子节点,动态模型中的模型也是如此,并且在动态模型中每一个XML节点都被当作是一个独立的模型,这样复杂的模型就可以通过简单的模型组合而成。

    比如下面是Person模型的定义。

<?xml version="1.0" encoding="utf-8"?>

<thing name="Person" descriptors="xworker.lang.MetaDescriptor3" modifier="">
    <attribute name="name" showLabel="" validateAllowBlank="" validateOnBlur="false" allowDecimals="false"
         allowNegative="false"></attribute>
    <attribute name="age" type="int" showLabel="" validateAllowBlank="" validateOnBlur="false"
         allowDecimals="false" allowNegative="false"></attribute>
    <thing name="Child" extends="xworker.example.otandoo.example.Person">
        <actions>
            <GroovyAction name="sayHello" varScope="Global" code="println &quot;Child: &quot; + self.name + &quot; say hello&quot;;"></GroovyAction>
        </actions>
    </thing>
    <actions>
        <GroovyAction name="whoAmI" varScope="Global" code="return &quot;I am a Person, my name is ${self.name}&quot;;"></GroovyAction>
        <GroovyAction name="sayHello" varScope="Global" code="println &quot;Preson: &quot; + self.name + &quot; say hello&quot;;"></GroovyAction>
    </actions>
</thing>

    比如上面的Person模型中定义了Person可以有name和age两个属性,有whoAmI和sayHello两个行为,它也定义Child子模型,子模型定义了sayHello行为。

    使用上面的模型作为定义,可以编写下面的模型。

<?xml version="1.0" encoding="utf-8"?>

<Person name="Zhangsan" descriptors="xworker.example.otandoo.example.Person" age="40">
    <Child name="XiaoMing" age="10"></Child>
</Person>

2.通过继承构建复杂的模型

    在上面的Person模型中,Child子模型继承了Person模型(见Child的extends属性),这样Child也继承了name和age两个属性,以及whoAmI和sayHello两个行为。

    由于Person和Child都定义了sayHello方法,这样对于Child来说相当于重写了Person的sayHello方法,在实力<Child name="XiaoMing" .../>模型执行sayHello方法时执行的是Child节点所定义的sayHello方法。

3.运行示例

    上面的示例已经集成在XWorker中的,在控制台下可以通过下面的命令来执行。

//执行模型Zhangsan的sayHello方法
dml xworker.example.otandoo.example.Zhangsan sayHello

//执行模型XiaoMing的sayHello方法
dml xworker.example.otandoo.example.Zhangsan/@XiaoMing sayHello

    执行结果如下。

 

Copyright ©  2007-2014 XWorker.org  版权所有

沪ICP备08000575号