不完全是单身人士
我正在使用 ActionScript3 为我团队中另一位开发人员正在开发的应用程序开发数据访问层。其中一个对象(我们称之为User
)的构造成本很高。每个用户都有一个唯一的 ID 号,因此我可以判断他们之前是否已创建,并且所有 User
对象都存储在其他位置的数组中。
我希望能够对其进行限制,以便每个 ID 只能有一个 User
实例。 IE 中每个用户都必须是唯一的,并且对已构造的 User
的请求应该接收预先构造的 User
。
如果我使用一种理智的、设计良好的语言,我会简单地将其设为私有构造函数,并强制对该对象的所有请求都通过一个缓存事物的函数。但Flash不允许私有构造函数(只有公共和内部构造函数,这两者都不起作用)。我怎样才能实现这个目标?
I'm using ActionScript3 to develop a Data Access Layer for an application another developer is working on in my team. One of the objects, lets call it User
is expensive to construct. Each user has a unique ID number so I can tell if they've been created before or not, and all User
objects are stored in an array somewhere else.
I want to be able to restrict it so that there can be only once instance of a User
per ID. I.E. each user has to be unique, and requests for an already constructed User
should receive the pre-constructed User
.
If I was using a sane, well-designed language I would simple make it a private constructor and force all requests for the object to go through a function that cached things. But flash doesn't allow private constructors (only public and internal, neither of which will work). How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想做私有构造函数,那么你可以这样实现:
If you want to do the private construtor then you can achieve this like this:
您可以使用字典来存储映射到 User 实例的用户 ID。这样,您可以在创建新用户之前检查是否已经存在现有用户。这是一些伪代码:
You could use a dictionary to store user ids mapped to User instances. This way you could check to see if there was already an existing user before creating a new one. Here is some pseudo code: