AS3 中的值对象和通用类有什么区别?

发布于 2024-08-17 19:09:34 字数 92 浏览 4 评论 0原文

我不明白 ActionScript3 中的值对象和类之间在结构上有何不同。

如果您决定将任何类称为 VO,那么它可以是 VO 吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2024-08-24 19:09:34

值对象 (VO) 是一种用于在软件应用程序子系统之间传输数据的设计模式。

动作脚本类可以包含字段(var)、属性(getter/setter)和方法。术语“值对象”是指框架(例如 Cairngorm)用于存储的类并在组件和模块之间传递数据。这些类充当保存数据的模板,通常不包含除 getter/setter 之外的函数。

Cairngorm 框架有一个 IValueObject 接口,该接口不支持不包括任何方法。

它是一个标记接口,通过识别 Cairngorm 应用程序中的类来提高代码的可读性,这些类将用作在应用程序层之间传递数据的值对象。

值对象是动作脚本中的一个宽松术语。 此处的 AS3 语言参考已使用此术语来表示对象传递给类的构造函数来初始化其属性。

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

使用值对象使类的用户能够仅初始化他们希望初始化的属性。此方法的另一种替代方法(在我看来更健壮且不易出错)是将每个属性指定为构造函数的参数并为其分配默认值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文