AS3:Singleton 类与 LocalConnection 类
我有一个加载 2 SWF 的主类(加载器和查看器,还包含文档类)。他们需要与内容共享一个双缓冲区,当然,由加载器填充并由查看器显示
我正在考虑使用 LocalConnection 类,但在 PatrickS 的建议后,现在我正在评估 Singleton 类的可能性。我从未在 AS 中使用过这种模式,并且必须承认我对它有相当的偏见。但在这种特殊情况下,我想它会很有用。顺便说一下,阅读 gskinner 博客中的 2 个实现示例有点惊讶。所以,我真的很欣赏你的观点,因为我知道这个主题是一场无休止的战争,就像 Mac 与 PC 之间的战争一样,
考虑到: 1. AIR 桌面应用程序在高端 Windows PC 上连续几个月 24x7 运行。无用户交互 2.高性能代码是必须的,因为加载的内容是全高清图像
我的另一个担忧是内存泄漏
提前致谢
I have a Main class loading 2 SWF (loader and viewer, also with document classes). They need to share a double buffer with content, of course, filled by loader and showed by viewer
I was thinking to use the LocalConnection class but after a suggestion from PatrickS now I'm evaluating the possibility of a Singleton Class. I've never used this pattern in AS and must confess I'm rather biased against it. But in this particular case I guess it'll be useful. By the way, a little bit surprised reading in the gskinner blog 2 implementations examples. So, I'll really appreciate your views, knowing this subject is an endless war like the Mac vs PC one
Take into account:
1. AIR desktop application running 24x7 during some months in a high-end Windows PC. No user interaction
2. High performance code is a must because content loaded are full HD images
My other concern is about memory leaks
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我会避免使用 Singleton,因为它会定期实现。但是,拥有此缓冲区对象的单个实例是有意义的。所以在这种情况下我要做的是让你的 Main 类创建这个对象。当查看器加载时,将这个对象传递给它。然后对装载机执行相同的操作。现在,您的两个 swf 共享一个可用于通信的公共对象。然后,您可以在其上调用函数并根据需要使其调度事件(扩展 EventDispatcher 或实现 IEventDispatcher)。或者,您可以根据需要注册回调(应该会快一点,但不确定是否会产生很大的影响)。
编辑
看看您的其他问题,我认为您的问题与获取正确的加载器并将数据传递到加载的内容有关。以下是如何实现我上面提到的内容的概述。
PhotoLoader.swf:
PhotoViewer.swf:
Main.swf
以及由 3 个 swf 共享的公共对象:
基本上,当您加载查看器和加载器 swf 时,您会传递此公共对象的一个实例。然后您注册监听 INIT 方法:这将告诉加载器和查看器一切都已设置完毕。因此,此时您可以开始从查看器或加载器向另一方发送消息(您可以通过事件调度来实现这一点);基本上在公共对象中创建一个方法来为您执行此操作,或者直接在公共对象上调用dispatchEvent。
I'd avoid a Singleton, personally, as its regularly implemented. However, having a single instance of this buffer object makes sense. So what I'd do in this case is have your Main class created this object. When the viewer is loaded, pass it this object. Then do the same for the loader. Now, both of your swfs share a common object that they could use to communicate. Then you can call functions on it and make it dispatch events if you want (extending EventDispatcher or implementing IEventDispatcher). Or you can just register callbacks if you want (should be a bit faster, but not sure if it will make a big difference).
Edit
Having a look at your other question I think your problem was related to getting the right loader and passing data to the loaded content. Here's an skecth of how you could implement what I mentioned above.
PhotoLoader.swf:
PhotoViewer.swf:
Main.swf
And the common object, shared by the 3 swf:
Basically when you load both your viewer and loader swfs you pass an instance of this common object. Then you register to listen for an INIT method: this will tell loader and viewer that everything has been setup. So at this point you can start sending messages from your viewer or loader to the other party (you could implement this through event dispatching); basically make a method in the common object to do it for you or just call dispatchEvent on the common object directly.