OOP:最佳实践:让两个班级互相交流

时间:2022-10-15 19:16:34

So, I'm building stuff in AS3, I have a main movie clip, and I have one set of navigation to be displayed twice (one is a normal side bar setup, the other is a fancy designer scrolly thing down the bottom). When one is clicked, the other should react to reflect what has happened.

所以,我正在AS3中构建东西,我有一个主要的影片剪辑,我有一组导航要显示两次(一个是正常的侧栏设置,另一个是一个花哨的设计师滚动底部的东西)。当点击一个时,另一个应该做出反应以反映发生的事情。

Should main.menuA reference main.menuB, or should I set up EventDispatchers and EventListeners so the event routes through main? or when I initiate menuA and menuB, should I pass a reference of menuA to menuB and viceversa?

main.menu应该引用main.menuB,还是应该设置EventDispatchers和EventListeners,以便事件通过main路由?或者当我启动menuA和menuB时,我应该将menuA的引用传递给menuB而反之吗?

Honestly, in this case, it doesn't matter too much, but I need to stop bad practices from becoming bad habits.

老实说,在这种情况下,它并不重要,但我需要阻止不良做法成为坏习惯。

2 个解决方案

#1


It makes most sense to setup events and change the corresponding control through main. If you did it the other way then each control would always have to be paired with the other, they shouldn't even be aware of each other.

最有意义的是设置事件并通过main更改相应的控件。如果你以另一种方式做到这一点,那么每个控件总是必须与另一个控件配对,他们甚至不应该彼此意识到。

But I have a slight change. You have a concept (PlayingStatus). This concept is independent of the way you draw it on the screen (obviously since you have two ways of doing it). Let's call them Layout1 and Layout2.

但我有一点变化。你有一个概念(PlayingStatus)。这个概念与你在屏幕上绘制它的方式无关(显然,因为你有两种方法)。我们称它们为Layout1和Layout2。

Both Layouts should hold a reference to the same PlayingStatus data. When one Layout modifies PlayingStatus, the PlayingStatus class can raise an event for each property (whatever those are) as it changes. Both Layouts would be listening for these events and redraw themselves when one arrives.

两个布局都应该保持对相同的PlayingStatus数据的引用。当一个Layout修改PlayingStatus时,PlayingStatus类可以为每个属性(无论是那些属性)引发一个事件,因为它会发生变化。两个布局都会监听这些事件,并在一个人到达时重绘自己。

#2


I'd recommend you go with event listeners and dispatchers. If your events bubble (and you set the scope for the listener to be on the "bubble path"), you don't need a direct reference from either button to the other.

我建议你选择赛事听众和调度员。如果您的事件冒泡(并且您将侦听器的范围设置为“气泡路径”),则不需要从任一按钮到另一个按钮的直接引用。

Just make sure you clean up the events when you destroy the objects, or (and) use weak references.

只需确保在销毁对象时清理事件,或者(和)使用弱引用。

#1


It makes most sense to setup events and change the corresponding control through main. If you did it the other way then each control would always have to be paired with the other, they shouldn't even be aware of each other.

最有意义的是设置事件并通过main更改相应的控件。如果你以另一种方式做到这一点,那么每个控件总是必须与另一个控件配对,他们甚至不应该彼此意识到。

But I have a slight change. You have a concept (PlayingStatus). This concept is independent of the way you draw it on the screen (obviously since you have two ways of doing it). Let's call them Layout1 and Layout2.

但我有一点变化。你有一个概念(PlayingStatus)。这个概念与你在屏幕上绘制它的方式无关(显然,因为你有两种方法)。我们称它们为Layout1和Layout2。

Both Layouts should hold a reference to the same PlayingStatus data. When one Layout modifies PlayingStatus, the PlayingStatus class can raise an event for each property (whatever those are) as it changes. Both Layouts would be listening for these events and redraw themselves when one arrives.

两个布局都应该保持对相同的PlayingStatus数据的引用。当一个Layout修改PlayingStatus时,PlayingStatus类可以为每个属性(无论是那些属性)引发一个事件,因为它会发生变化。两个布局都会监听这些事件,并在一个人到达时重绘自己。

#2


I'd recommend you go with event listeners and dispatchers. If your events bubble (and you set the scope for the listener to be on the "bubble path"), you don't need a direct reference from either button to the other.

我建议你选择赛事听众和调度员。如果您的事件冒泡(并且您将侦听器的范围设置为“气泡路径”),则不需要从任一按钮到另一个按钮的直接引用。

Just make sure you clean up the events when you destroy the objects, or (and) use weak references.

只需确保在销毁对象时清理事件,或者(和)使用弱引用。