开发者

Disadvantages of using a value object in construction and initialisation

开发者 https://www.devze.com 2023-03-09 02:36 出处:网络
I want to know if there are any disadvantages/ drawbacks of using value objects in initializing an object, for example:

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;
    }
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消