哪个是更好的实现?使用 Parsley 框架进行 Flex 编程
我对事件驱动编程和使用 MVC 相当陌生,所以如果这个问题看起来不是问题,请原谅我。
我正在尝试为我的单例模型类(客户端)分配值。我有 2 个选项:
我可以使用模型类构造函数来分配给自身,如下所示:
<块引用>类客户端{
公共变量名称; 公共 var 电子邮件; 公共函数客户端(arg_name,arg_email){ this.name = arg_name; this.email = arg_email; } }
我可以使用控制器为我分配值,如下所示:
<块引用>类控制器{ 公共 var client:Client = new Client(); 公共函数分配(){ client.name = "嘘"; client.email = "[电子邮件受保护]"; }
}
哪一个是更好的解决方案? :) 我如此困惑的原因是因为我见过只将值传递给模型类并执行 #1 的示例(并设置新变量,例如 [var fullname = fname + lname],但我知道事实上,控制器的工作就是为模型分配值。
I'm fairly new at event driven programming and using MVC so forgive me if this question seems like a non-question.
I'm trying to assign values to my singleton model class (Client). I have 2 options:
I can use the model class constructor to assign to itself like so:
Class Client{
public var name; public var email; public function Client(arg_name, arg_email){ this.name = arg_name; this.email = arg_email; } }
I can use the controller to assign my values for me like so:
Class Controller{ public var client:Client = new Client(); public function assign(){ client.name = "booo"; client.email = "[email protected]"; }
}
Which one of these is a better solution? :) The reason why I'm so confused is cause I've seen examples that just pass values to the model class and do #1 (and setting new variables such as [var fullname = fname + lname], yet I know for a fact that it is the controller's job to assign values to the model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更好是相对的。
第二个例子就是通常所说的“属性注入”。
基本上第一个选项更快、更小、代码更少。
第二种选择更具可读性和灵活性。
无论控制器如何,注入的好处是您可以为每个属性创建 getter 和 setter。
所以基本上你可以发生一些事情,例如每当你更改“name”属性的值时调度一个事件。
Flex的源代码充满了这个概念。例如,每当您更改组的宽度时,它不仅会更改值,还会执行检查,然后将组设置为脏组,以便其他事物可以对其做出反应。
基本上,它使得让所有事情都做自己的事情变得简单,并且它更加面向对象。
所有这些并不意味着它实际上更好,但现在以第二种选项样式编写代码更常见。
希望有帮助。
Better is relative.
The second example is what is commonly called "property injection".
Basically the first option is quicker, smaller, and much less code.
The second option is more readable and flexible.
Regardless of the Controller, injection has the benefit that you can create getters and setters for each property.
So basically you can have something happen such as dispatching an event whenever you change the value of the "name" property.
The flex source code is filled with this concept. For example whenever you change the width of a group, it doesn't just change the value, it performs checks and then sets the group as dirty so that other things can react to it.
Basically it makes it simple to have everything do it's own thing, and it's more Object oriented.
All that doesn't mean that it's actually better, but it is more common now to write code in the second option style.
Hope that helps.