views:

282

answers:

1

I don't understand what is structurally different between a Value Object and a Class in ActionScript3.

Can any Class be a VO if you decide to call it one?

Thanks.

+1  A: 

Value Object (VO) is a design pattern used to transfer data between software application subsystems.

An actionscript class can include fields (vars), properties (getters/setters) and methods. The term value object refers to classes used by frameworks (such as Cairngorm) to store and pass data among components and modules. These classes act as templates to hold data and generally won't contain functions other than getters/setters.

The Cairngorm framework has a IValueObject interface that doesn't include any methods.

It is a marker interface that improves readability of code by identifying the classes within a Cairngorm application that are to be used as value objects for passing data between tiers of an application.

Value object is a loose term in actionscript. The AS3 language reference here has used this term for an object that's passed to the constructor of a class to initialize its properties.

class Circle extends Shape 
{
    public var bgColor:Number = 0xFFFFFF;
    public var radius:Number = 0;
    public var xCenter:Number = 0;
    public var yCenter:Number = 0;

    public function Circle(initObj:Object) 
    {
        //initialize properties from the value object.
        for(var i:String in initObj) 
        {
            this[i] = initObj[i];
        }
        draw();
    }

    public function draw():void 
    {
        graphics.beginFill(bgColor);
        graphics.drawCircle(xCenter, yCenter, radius);
        graphics.endFill();
    }
}

var firstInitObj:Object = new Object();
firstInitObj.bgColor = 0xFF0000;
firstInitObj.radius = 25;
firstInitObj.xCenter = 25;
firstInitObj.yCenter = 25;
//pass the value object to the constructor.                    
var firstCircle:Circle = new Circle(firstInitObj);

The use of value object enable the users of the class to initialize only those properties that they wish to. An alternative to this method (more robust and less error prone one imo) is to specify each property as arguments to the constructor and assign default values to them.

Amarghosh