不完全是单身人士

发布于 2024-09-11 03:01:29 字数 415 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2024-09-18 03:01:29

如果你想做私有构造函数,那么你可以这样实现:

package somepackage {
  public class User {
    public function User(SingletonEnforcer s):void {
      if (s == null)
        throw new Exception("Sorry mate!!!");
    }
    public static GetUserWithId(id:String):User {
      //user with this id exists then return it.
      //create a new user, initialize it, cache it and return
    }
  }
}
class SingletonEnforcer {
  //this is only visible to this class (User).
}

If you want to do the private construtor then you can achieve this like this:

package somepackage {
  public class User {
    public function User(SingletonEnforcer s):void {
      if (s == null)
        throw new Exception("Sorry mate!!!");
    }
    public static GetUserWithId(id:String):User {
      //user with this id exists then return it.
      //create a new user, initialize it, cache it and return
    }
  }
}
class SingletonEnforcer {
  //this is only visible to this class (User).
}

微凉 2024-09-18 03:01:29

您可以使用字典来存储映射到 User 实例的用户 ID。这样,您可以在创建新用户之前检查是否已经存在现有用户。这是一些伪代码:

public class UserLookup()
{
    var _lookup:Dictionary = new Dictionary();

    function getUser( id:int ):User 
    {
        if( _lookup[id] ) {
            return _lookup[id]
        } else {
            var user = new User( id );
            _lookup[ id ] = user;
            return user;
        }
    }
}

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:

public class UserLookup()
{
    var _lookup:Dictionary = new Dictionary();

    function getUser( id:int ):User 
    {
        if( _lookup[id] ) {
            return _lookup[id]
        } else {
            var user = new User( id );
            _lookup[ id ] = user;
            return user;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文