In this post I somewhat extended the previous example to show you how to work with a properties.txt file and interface-based programming.
Example (right click swf to see source)
The application-context-johlero.xml looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <?xml version="1.0"?> <objects xmlns="http://www.pranaframework.org/objects" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.pranaframework.org/objects http://www.pranaframework.org/schema/objects/prana-objects-0.6.xsd" > <property file="application-context-johlero.properties.txt" /> <!-- Create an instance of a class implementing ICalculationFactory interface --> <!-- You can switch between MultiplyFactory and DivisionFactory to see the difference --> <!-- object id="multiplyFactory" class="johlero.factory.MultiplyFactory"/--> <object id="divisionFactory" class="johlero.factory.DivisionFactory"/> <!-- ================================================ --> <!-- Singleton --> <!-- Prana will call getInstance() function on instance of johlero.model.JohleroModelLocator --> <!-- Next Prana will set the four variables --> <!-- ================================================ --> <object id="johleroModelLocator" class="johlero.model.JohleroModelLocator" factory-method="getInstance"> <property name="selected" value="true" /> <property name="title" value="Johlero Prana Framework Example" /> <property name="fontColor" value="0xFC0C06"/> <property name="fontSize" value="40"/> <!-- the ref attribute is a reference to previous defined instance --> <property name="calculationFactory" ref="divisionFactory"/> <!-- Next property is assigned a value that is defined in --> <!-- application-context-johlero.properties.txt --> <property name="gatewayUrl" value="${gatewayUrl}"/> <!-- Next Commented lines are another way of assigning an object to --> <!-- the calculationFactory property --> <!--<property name="calculationFactory">--> <!--<object class="johlero.factory.DivisionFactory"/>--> <!--</property>--> </object> </objects> |
There are two new things here. First is the reference to a property file application-context-johlero.properties.txt (containing key value pairs) and the assignment of an instance of a class to a property. Notice that the property calculationFactory in JohleroModelLocator can be set to an instance of DivisionFactory or MultiplyFactory in two ways.
In my experience using a property file is usefull to seperate program specific configuration from environment specific configuration. In my example a have a gatewayUrl which is defined in the property file because this gatewayUrl will probably change when deploying on another server.
To learn some things about interface based programming I suggest you read a book about Design Patterns (Head First, Gang of Four) and OOP (Head First) but you can see that Prana is a great way of keeping your application kind of stupid. The application doesn’t know if a division of multiplying will be executed. Only at runtime an instance of DivisionFactory or MultiplyFactory is injected in the application. Because the application doesn’t have a reference to DivisionFactory or MultiplyFactory (only a reference to ICalculationFactory exists) you need to add these Classes to your application. If you don’t do this, Prana will not be able to create an instance of these classes at runtime.
That’s why you’ll see in the PranaExample2.mxml:
1 2 3 4 | {
MultiplyFactory,
DivisionFactory
} |
Well, I think this is about all the explanation you’ll need. The example speaks for itself.
Ciao!