Singleton pattern interview

前端 未结 8 2165
忘了有多久
忘了有多久 2021-02-04 14:26

I am recently asked about java related question in an interview with following code, since I am very new to java and barely code in Java so I really have no idea what the follow

相关标签:
8条回答
  • 2021-02-04 14:32

    Original Answer is that only one instance of Bolton is created.

    0 讨论(0)
  • 2021-02-04 14:33

    Interviewer basically wants to check your knoweldge of Singleton pattern . Can the pattern be broken?. Ans is Yes. Check this or google - when singleton is not a singleton.

    Best course is to use Enum based Singleton as suggested by Joshua Bloch

    0 讨论(0)
  • 2021-02-04 14:34

    This is a Singleton Pattern

    The idea of a Singleton Pattern is to only have one available instance of a class. Therefore the constructor is set to private and the class maintains, in this case, a getInstance() method that either calls an existing instance variable, INST in this class, or creates a new one for the executing program. The answer is probably 1, because it's not thread safe. It may be confused for 3, which I had put down earlier, but that is by design, technically, so not actually a flaw.

    Here's an example of Lazy Initialization, thread-safe singleton pattern from Wikipedia:

    public class SingletonDemo {
    
        private static volatile SingletonDemo instance = null;
    
        private SingletonDemo() {  } 
    
        public static SingletonDemo getInstance() {
            if (instance == null) {
                synchronized (SingletonDemo.class){
                    if (instance == null) {
                        instance = new SingletonDemo();
                    }
                }
            }
            return instance;
        }
    
    }
    

    Setting the instance variable to volatile tells Java to read it from memory and to not set it in cache.

    Synchronized statements or methods help with concurrency.

    Read more about double checked locking which is what happens for a "lazy initialization" singleton

    0 讨论(0)
  • 2021-02-04 14:35

    The getInstance() method should be synchronized, otherwise many instances could be created if multiple threads calls getInstance() at the same time. So I would select option 1.

    0 讨论(0)
  • 2021-02-04 14:35

    We use Singleton Pattern when we want to have only one object of this class and it will be used every where. So to restrict the class to create many objects, we should use private for constructor of this class. And create one public function to return the object of this class.

    public class MethodWin {
        private int isLoggedOn=0;
        private static MethodWin objectMethodWin = new MethodWin();
        private MethodWin() { }
        public static MethodWin getInstance() {
            return objectMethodWin;
        }
        public void setIsLoggedOn(int value) {
           this.isLoggedOn=value;
        }
        public int getIsLoggedOn() {
           return this.isLoggedOn;
        }
    }
    

    So when we need to create this obect, we should:

    MethodWin meth = MethodWin.getInstance();
    
    0 讨论(0)
  • 2021-02-04 14:41

    Through reflection we can create many objects even if the constructor is private.
    In multi-threaded environment there are chances to create more than one instance.
    Through serialization there are chances to create more than one object.

    0 讨论(0)
提交回复
热议问题