如何初始化我的单例对象

发布于 2024-12-04 05:59:38 字数 514 浏览 1 评论 0原文

public class FooClient {

    private Foo foo;
    private final static String key = "<api-key>";

    private static FooClient client = new FooClient();

    private FooClient() {
        foo = new Foo(key);
    }

    public static FooClient getFooClient() {
        return client;
    }
}
  1. 以上述方式初始化client可以吗?
  2. 我应该声明 private Foo foo;作为静态,我猜情况并非如此。
  3. 如果我必须支持不同键的不同单例,我是否应该修改 getFooClient(String key) 来获取键并缓存它,以便我可以返回特定于键的单例 FooClient。
public class FooClient {

    private Foo foo;
    private final static String key = "<api-key>";

    private static FooClient client = new FooClient();

    private FooClient() {
        foo = new Foo(key);
    }

    public static FooClient getFooClient() {
        return client;
    }
}
  1. Is it ok to initialize client in the above fashion.
  2. Should I declare private Foo foo; as static, I am guessing its not the case.
  3. If I have to support different singletons for different keys, should I modify getFooClient(String key) to take in a key and cache it, so that I can return singleton FooClients which are key specific.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

涙—继续流 2024-12-11 05:59:39

通常你声明

private static final FooClient client = new FooClient();

这是传统的单例实现。有关其他实施选项,请参阅维基百科页面

我不会将 Foo foo 声明为 static

如果您的单例可以根据键返回不同的实例,那么最好在 getFooClient() 方法中传递键值。

Usually you declare

private static final FooClient client = new FooClient();

This is the traditional Singleton implementation. See wikipedia page for other implementation options.

I would not declare Foo foo as static.

If your singleton can return different instances based on the key, then it's a good idea to pass the key value in the getFooClient() method.

夜未央樱花落 2024-12-11 05:59:39

如果你有不止一个东西,它就不是单例。

在这两种情况下我都会使用enum

对于只有一个的情况。

enum FooClient {
    INSTANCE;

    private final Foo foo = new Foo("<api-key>");
}

对于有多个的情况。

enum FooClient {
    INSTANCE1("<api-key>"), INSTANCE2("<api-key2>");

    private final Foo foo;
    FooClient(String apiKey) {
        foo = new Foo(apiKey);
    }
}

If you have more than one of something, its not a singleton.

I would use enum in both cases.

For the case where this is just one.

enum FooClient {
    INSTANCE;

    private final Foo foo = new Foo("<api-key>");
}

for the case where there is more than one.

enum FooClient {
    INSTANCE1("<api-key>"), INSTANCE2("<api-key2>");

    private final Foo foo;
    FooClient(String apiKey) {
        foo = new Foo(apiKey);
    }
}
影子的影子 2024-12-11 05:59:38
  1. 是的。在构造函数中,您可以检查是否 client != null ,如果是,则抛出错误。 (这将对抗反射实例化)

  2. 不,它是单例的实例字段

  3. 是。并且您应该有一个 Map。但请注意,这不是“不同的单身人士” - 您的单身人士是“客户端”。其他类可以多次实例化。

  1. Yes. In the constructor you can check if client != null and if it is - throw an error. (this will counter reflection instantiations)

  2. No, it is an instance field of the singleton

  3. Yes. And you should have a Map<String, Foo>. But note that that is not "different singletons" - your singleton is the "client". The other classes can be instantiated multiple times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文