3

I want to know if there are any disadvantages/ drawbacks of using value objects in initializing an object, for example:

public class MonsterVO
{
    public var tenticles : Boolean;
    public var growl : GrowlType;
    public var damage : int;
    public var health : int;

}

public class Monster
{
    private var tenticles : Boolean;
    private var growl : GrowlType;
    private var damage : int;
    private var health : int;

    public function Monster(monsterData : MonsterVO)
    {
        tenticles = monsterData.tenticles;
        growl = monsterData.growl.clone();
        damage = monsterData.damage;
        health = monsterData.health;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
serenskye
  • 3,467
  • 5
  • 35
  • 51
  • It really all depends what you're trying to accomplish. What is the context of where you'll use monster. Is this really your use case or is it just an example? Why wouldn't your monster know how many tentacles it already has? – J_A_X May 30 '11 at 20:21

2 Answers2

2

If you plan to use and initialize your Monster domain object the only way (from DTO MonsterVO) — there is no problem. But how you can be sure if there won't any other usages in the future? You can't overload constructor in ActionScript (like in Java). What if you'll need to create a clone? You'll have to create some fake MonsterVO for that :(

I think it is better to create some Factory Method to solve your problem.

Constantiner
  • 14,231
  • 4
  • 27
  • 34
0

If you want to use fa Factory- which I think is a good point- you could still use an initialization mechanism using a configuration object.

I would remove it from the constructor, thou.

If you implement a method like the following, you can change what you configure by changing the VO. This would be the simplest solution (not using Factory, or a DI like approach)

public function configure(config:MonsterVO):void {
   for (var prop :String in config) {
        if ( config[prop] != null && this.hasOwnProperty(prop) ) this[prop] = config[prop];             
    }
}

Notice that you would need to make your configurable options available as properties.

goliatone
  • 2,183
  • 16
  • 29