Well we have Groovy and we could use the Groovy builders as shown here. The Problem with the builders is that you might lose some realtime syntax checking, some tools already have. I want to talk about a different way to describe data. A combination of properties and closures to keep the syntax lean. Lets have a look at an example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestClass { | |
public TestClass(Closure closure) { | |
closure.setDelegate(this); | |
closure.run(); | |
} | |
} |
Normally we would have to generate the class and set the properties or we would have to define constructors with property lists. There is another way, we can use closures to set the properties dynamically at object creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String [] main) { | |
TestClass bla = new TestClass() { | |
helloWorld = "aaa"; | |
}; | |
} |
What we do here is to generate a new object and dynamically set helloWorld which has a predefined value of "helloworld" to add. Another way to do the same would be following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String [] main) { | |
TestClass bla = new TestClass(); | |
bla['helloWorld'] = "aaa" | |
} |
Or like following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String [] main) { | |
TestClass bla = new TestClass(helloWorld:"ccc"); | |
print bla.helloWorld; | |
} |
Same result different angle to the approach. What we do here is following we use the internal map semantics groovy uses to store properties to set the value of helloWorld to a new value. In the first example we do it explicitely in the second one via dynamic parameter maps for the constructor. Both cases would result in the same result! Lets have a look on how compact this approach is:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List retVal = new LinkedList(); | |
def component = new Component() { | |
basepackage = "org.apache.myfaces.dojo.custom.form.textarea"; | |
tagname = "dijitTextarea"; | |
componentclass = "TextArea"; | |
tagclass = "TextAreaTag"; | |
renderclass = "TextAreaRenderer" ; | |
renderbaseclass = "org.apache.myfaces.dojo.custom.base.DijitInputRenderer"; | |
tagbaseclass = "org.apache.myfaces.dojo.custom.base.DijitInputTag"; | |
componentbaseclass = "org.apache.myfaces.dojo.custom.base.DijitInput"; | |
rendertype = "org.apache.myfaces.dojo.TextAreaRenderer"; | |
componenttype = "org.apache.myfaces.dojo.TextArea"; | |
}; | |
/*now to the component attributes map*/ | |
component.attributes.add(new Attribute() { | |
name = "rows"; attrclass = "Integer"; description = "Rows of the component"; | |
}); | |
retVal.add(component); | |
return retVal; |
Now think how much more code you would have in xml or how much more effort this would be by rolling your own data definition language. The good thing is you can set your properties to predefined defaults and then use the closures to override the values you want to change. The best thing however is you can use all this seamlessly from java. A Java Class simply would have to call the setters and getters of the properties of the generated objects to access those values! The groovy compiler takes care of the rest!
Keine Kommentare:
Kommentar veröffentlichen