Building a Method to Create an Object


In this task, you will build a static method that will accept an object, and return an instance of the Product class.

1.

Be sure the Product class in the valueObjects folder is open. Locate the toString() method and add the skeleton of a new public static method called buildProduct() after the method. Be sure that the return type of the method is set to Product and that it accepts an object parameter, as shown:

public static function buildProduct(o:Object):Product{ } 


A static method can be called without having to first create an object from the class. Instance methods, like those we have used so far, rely on the state of the object they are attached to. Methods that are declared as static need only rely on the class, not on any state of the object. Static methods create behavior that is completely independent of the object. Static methods are useful for utilities such as the buildObject() static method you are creating here. You need access to this method without having to create an object first. Used appropriately, static methods can increase performance because you do not have to first create an object that uses space in memory. To reference a static method with the name of getName() from the Product class, you would simply write this code:

Product.getName(); 


2.

Inside the buildProduct() method, instantiate an instance of the Product class with the name of p by using the new keyword. Set the catID, prodName, unitID, cost, listPrice, description, isOrganic, isLowFat, and imageName properties as parameters to the constructor. You will need to cast the isOrganic and isLowFat variables to Boolean.

[View full width]

var p:Product = new Product(o.catID, o.prodName, o.unitID, o.cost, o.listPrice, o .description, Boolean(o.isOrganic), Boolean(o.isLowFat), o.imageName);


Casting a variable tells the compiler to treat a given value as a specific type. In this example, we are telling the compiler that you, the developer, know that the isOrganic and isLowFat properties will contain Boolean data. Casting will be discussed in detail later; however, it is necessary here due to the problems discussed with untyped data and the <mx:Model> tag.

3.

Return the object you just created by using the return keyword with the name of the object p. Your final buildProduct() method should look as follows:

[View full width]

public static function buildProduct(o:Object):Product{ var p:Product = new Product(o.catID, o.prodName, o.unitID, o.cost, o.listPrice, o .description, Boolean(o.isOrganic), Boolean(o.isLowFat), o.imageName); return p; }


This code will return the value object created by passing a data structure to the method.

4.

Save the Product.as file.

The class file is saved with the new method. No errors should appear in the problems console.

5.

Return to EComm.mxml. In the prodHandler() method, remove the code that builds theProduct and replace it with code that uses the static method to build theProduct.

theProduct = Product.buildProduct(theItems); 


This code calls the method that builds an instance of the Product class, which returns a clean, strongly typed value object from a data structure that consisted of objects that were not typed and were loosely organized.

6.

Locate the first VBox layout container in the expanded state, which displays the product description, whether the product is organic and whether the product is low fat. Change the text property of <mx:Text> tag to reference the theProduct object you created in the prodHandler() method. Also, add a visible property to both labels, and bind each to the appropriate theProduct object properties, as follows:

<mx:VBox width="100%" x="200">    <mx:Text text="{theProduct.description}"       width="50%"/>    <mx:Label       text="Certified Organic"       visible="{theProduct.isOrganic}"/>    <mx:Label       text="Low Fat"       visible="{theProduct.isLowFat}"/> </mx:VBox> 


You are now referencing the value object you created. Notice that the ActionScript class is self-documenting; because you are using typed properties, they appear as code hints.

7.

Save and debug the application.

You should see the trace functions just as before and that the data binding is still working when you roll over the image.




Adobe Flex 2.Training from the Source
Adobe Flex 2: Training from the Source
ISBN: 032142316X
EAN: 2147483647
Year: 2006
Pages: 225

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net