Java Singleton设计模式:问题 [英] Java Singleton Design Pattern : Questions

查看:95
本文介绍了Java Singleton设计模式:问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我接受过一次采访,他问我有关Singleton Design Patterns是如何实现的,我告诉他,使用静态变量和静态方法,我们可以实现Singleton设计模式。



他似乎对答案感到满意,但我想知道


  1. 我们可以使用多少种不同的方式
    在Java中实现Singleton设计模式

  2. Singleton Object的范围是什么,它在JVM中如何实际工作?我知道我们总是会有一个Singleton对象的实例,但该对象的实际范围是什么,是JVM还是多个应用程序运行,而JVM中的每个上下文基础的范围呢?无法给出令人满意的解释?

  3. 最后他问是否有可能使用具有集群的Singleton对象进行解释,有什么办法让Spring不实现Singleton Design Pattern调用Bean Factory获取对象?

任何投入都将高度赞赏Singleton,并且要记住的主要内容处理单身人士?



谢谢。

解决方案

有几种方法可以在Java中实现Singleton模式:



public private instance
//用法:Blah.INSTANCE.someMethod();
public class Blah {
public static final Blah INSTANCE = new Blah();
private Blah(){
}
//公共方法
}

//私有构造函数,公共实例方法
//用法:Woo.getInstance()。someMethod();
public class Woo {
private static final Woo INSTANCE = new Woo();
private Woo(){
}
public static Woo getInstance(){
return INSTANCE;
}
//公共方法
}

// Java5 +单元素枚举(首选方法)
//用法:Zing.INSTANCE.someMethod );
public enum Zing {
INSTANCE;
//公共方法
}

给出上面的例子,你将有每个类加载器的单个实例。



关于在集群中使用单例...我不知道使用的定义是...是面试官意味着在集群中创建单个实例?我不知道这是否有很大的意义?



最后,在spring中定义一个非单例对象是通过属性singleton = false。


I had an interview recently and he asked me about Singleton Design Patterns about how are they implemented and I told him that using static variables and static methods we can implement Singleton Design Patterns.

He seems to be half satisfied with the answer but I want to know

  1. How many different ways we can implement Singleton Design Pattern in Java ?
  2. What is the scope of Singleton Object and how does it actually work inside JVM ? I know we would always have one instance of Singleton Object but what is the actual scope of that object, is it in JVM or if there are multiple application running than it's scope is per context basis inside the JVM, I was really stumped at this and was unable to give satisfying explanation ?
  3. Lastly he asked if it is possible to used Singleton Object with Clusters with explanation and is there any way to have Spring not implement Singleton Design Pattern when we make a call to Bean Factory to get the objects ?

Any inputs would be highly appreciated about Singleton and what are the main things to keep in mind while dealing with Singletons ?

Thanks.

解决方案

There are a few ways to implement a Singleton pattern in Java:

// private constructor, public static instance
// usage: Blah.INSTANCE.someMethod();
public class Blah {
    public static final Blah INSTANCE = new Blah();
    private Blah() {
    }
    // public methods
}

// private constructor, public instance method
// usage: Woo.getInstance().someMethod();
public class Woo {
    private static final Woo INSTANCE = new Woo();
    private Woo() {
    }
    public static Woo getInstance() {
        return INSTANCE;
    }
    // public methods
}

// Java5+ single element enumeration (preferred approach)
// usage: Zing.INSTANCE.someMethod();
public enum Zing {
    INSTANCE;
    // public methods
}

Given the examples above, you will have a single instance per classloader.

Regarding using a singleton in a cluster...I'm not sure what the definition of "using" is...is the interviewer implying that a single instance is created across the cluster? I'm not sure if that makes a whole lot of sense...?

Lastly, defining a non-singleton object in spring is done simply via the attribute singleton="false".

这篇关于Java Singleton设计模式:问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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