The Value Object Pattern

I been trying to explain to a friend how to use Value Objects to describe data, after a google search returned nothing more than an ancient DevNet article by Branden Hall, I figured I'd better write a quick example.

So the first thing I do is fire up FlexBuilder and create a new project, source folder and a package called VO. In that package I create a really simple Value Object which describes a Car and 3 properties of the Car. These properties are public vars and typed to the data type that we want to handle. Quite often Value Objects contain arrays of other Value Objects.

ACTIONSCRIPT:
  1. package VO
  2. {
  3.     public class CarVO
  4.     {
  5.         public var colour : String;
  6.         public var numWheels : int;
  7.         public var price : String;
  8.     }
  9. }

Once we have out VO setup we want to do something with it:

ACTIONSCRIPT:
  1. package {
  2.     import flash.display.Sprite;
  3.     import VO.CarVO;
  4.  
  5.     public class VO_Example_Project extends Sprite
  6.     {
  7.        
  8.         private var _dummyXML : XML;
  9.         private var _arrayOfVOs : Array;
  10.        
  11.         public function VO_Example_Project()
  12.         {
  13.             _dummyXML = new XML();
  14.             _dummyXML = <data>
  15.                 <car colour="red" wheels="4" price="34,531" />
  16.                 <car colour="purple" wheels="3" price="4,444" />
  17.                 <car colour="brown" wheels="6" price="12,667" />
  18.             </data>;
  19.            
  20.             pushDataToVOs ( _dummyXML );
  21.             var tempCar : CarVO = CarVO ( _arrayOfVOs [ 1 ] );
  22.             trace ( "what's the price of the 2nd car in the array", tempCar.price );
  23.         }
  24.        
  25.         private function pushDataToVOs ( data : XML ) : void
  26.         {
  27.             _arrayOfVOs = [ ] ;
  28.             for ( var i : int = 0; i <data.*.length ( ) ; i ++ )
  29.             {
  30.                 var carVO : CarVO = new CarVO ( ) ;
  31.                 carVO.colour = data.car[ i ].@colour ;
  32.                 carVO.numWheels = data.car[ i ].@wheels ;
  33.                 carVO.price = data.car[ i ].@price ;
  34.                 _arrayOfVOs.push( carVO );
  35.             }
  36.         }
  37.     }
  38. }

So, what's going on there?

First we declare some vars we'll be using later, in our constructor we create an XML instance and give it some dummy data, E4X allows us to define this inline with our Actionscript. Then we pass the XML to a method that's going to push the data to Value Objects and store it in our Array.

The first thing the method does is instantiate our array. Then we loop through our XML based on it's length, for each loop we create a temporary var carVO which is of type CarVO, and add the values from the XML to the corresponding property of our Value Object. Once all the properties are in we push it to our array.

On lines 21-22 I show how you can then access that data to get he values back out.

The beauty of this technique is that on line 22 I can type "tempCar." and then auto-complete and FlexBuilder will give me a list of all the properties of CarVO.

Feel free to download a zip of the files and let me know if it's of any use jolyonruss [a] gmail.com

Socialise this post, find out more about the Socializer plugin for WordPress.