开发者

ActionScript - Should Event Dispatching/Listening Be Avoided Where Possible?

开发者 https://www.devze.com 2023-02-01 05:51 出处:网络
recently, i ran into a problem where i needed to access the List object from the List\'s custom cell renderer class.

recently, i ran into a problem where i needed to access the List object from the List's custom cell renderer class.

there were 2 options:

  1. listen for and dispatch a custom event to communicate between the 2 classes.
  2. reference the List from the cell renderer class with the parent property: List(parent.parent.parent)

while it's much easier to choose the second option, i feel that dispatching and listening for a custom event would result in code that's more inline with the design of AVM2, offers greater control for communication and, as it's expected AS3, should be less difficult to debug or maintain within new hands.

however, i also feel that using an event is more expensive, requires attention to resource management perhaps making it more difficult to debug and maintain and could be generally overkill.

is this simply a matter of 开发者_运维百科needs or taste? should dispatching/listening for custom events be avoided if they can be?


Don't forget that you can't see the control flow of event listeners from single glances at the code.

While I'm not against event listeners completely, if you use them, you should try to use them in the most self-documenting and simple way possible. If you have clever stuff, like adding/removing event listeners, and it goes wrong, then it can be a nightmare to debug because you cannot see what dispatchEvent is going to do.

If owner gets you the right object, then I would just go with that, myself.


I would look at it from another angle. If the other option ends up creating more dependency between the two classes, I would definitely opt for event dispatching.

In that regard , I'm not sure why you think that an event driven application would be harder to debug & maintain. It would seem to be the opposite, but it would of course depends on how one implements the event dispatching in the application. But saying this and re-reading your question I realize you seem to contradict yourself! Of course , I tend to agree with the first statement.

...offers greater control for communication and, as it's expected AS3, 
should be less difficult to debug or maintain... 

...perhaps making it more difficult to debug and maintain and 
could be generally overkill.
0

精彩评论

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

关注公众号