使用单例编写的 AIR 应用程序需要生成多个应用程序实例 [英] AIR Application written using Singletons needs to spawn multiple instances of app

查看:35
本文介绍了使用单例编写的 AIR 应用程序需要生成多个应用程序实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 AIR 应用程序,它使用单例来管理整个应用程序的全局状态.非常适合它的工作(管理网络设备).但现在客户想要从第一个实例生成多个应用程序实例,以便他们可以同时管理多个硬件设备......有点像仪表板,但要大得多.

I have an AIR application that uses Singletons to manage global state throughout the application. Works great for what it does (manages a network appliance). But now the client wants to spawn multiple application instances from the first instance so they can manage multiple hardware devices at the same time ... kind of like a dashboard but lots bigger.

你可以猜到,我用单例将自己编码到一个角落里,所以如果我重构应用程序以拥有一个引导类,它用一个按钮启动应用程序逻辑,比方说,它会产生另一个包含相同应用程序逻辑的窗口,然后在两个应用程序窗口之间共享单例,而我希望有两个单独的实例.

As you can guess, I coded myself into a corner with the singletons so if I refactor the application to have a bootstrap class that launches the application logic with a button, let's say, that spawns another window that contains the same application logic, then the singletons are shared across both app windows whereas I would have liked two separate instances.

有没有办法使用单例(或其他东西)来保持全局状态,但在一个应用程序中创建多个实例,每个实例都需要自己的状态管理器?

Is there a way to use singletons ( or something else ) to keep global state but create multiple instances in one application where each instance needs it's own state manager?

推荐答案

有几个不同的选项:

  1. 将每个应用程序实例加载到它自己的ApplicationDomain
  2. 在您不使用的地方使用依赖项注入框架之一't get the singleton - 而不是它的值在运行时分配给您的类属性.所有的东西都由元数据管理.这将需要一些代码更改.
  3. 重构您的代码,以便每个类实例都知道它的上下文.这可以通过添加构造函数参数或 setContext(value:Context) 函数来实现.Context 实例将包含指向所有前单身人士的链接.
  4. 向上移动显示列表,直到找到一些可以为您提供前单身人士链接的类实例.确保每个非显示列表对象都有一个指向显示列表对象的链接,以获取单例.
  1. Load each application instance into it's own ApplicationDomain
  2. Use one of the dependency injection frameworks where you don't get the singleton - instead of this it's value is assigned to your class property in runtime. All the stuff is managed by the metadata. It would require some code changes.
  3. Refactor your code so that each class instance would know it's context. This can be implementing by adding a constructor parameter or setContext(value:Context) function. Context instance will contain links to all your ex-singletons.
  4. Go up the display list until you find some class instance that can provide you the links to ex-singletons. Make sure that each non-display-list object has a link to display list object to get singletons too.

这篇关于使用单例编写的 AIR 应用程序需要生成多个应用程序实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆