Why is it mandatory to have private Constructor inside a Singleton class

前端 未结 10 1117
心在旅途
心在旅途 2020-12-10 08:12

This is my singleton class for obtaining the database connection.

I have a question here: why it compulsory to have a private constructor inside a singleton class (a

相关标签:
10条回答
  • 2020-12-10 08:33

    The comment as you said that If I am complete owner of my application and I will never commit mistake of creating instance of singleton class directly using the public constructor but will use the static method for getting it. But in real world, often application switch hands between multiple developers. If the new developer is not aware that you want only one instance of the class in the application, he/she may accidently create another one by using the public constructor.

    0 讨论(0)
  • 2020-12-10 08:34

    Otherwise everyone can create an instance of your class, so it's not a singleton any more. For a singleton by definition there can exist only one instance.

    0 讨论(0)
  • 2020-12-10 08:35

    The Singleton pattern typically includes a non-public constructor, for two reasons. A constructor has to exist because if there's no constructor at all, a public default constructor is included. If you have a public constructor, though, people can just make their own singletons at will (which someone inevitably will, meaning there can be more than one).

    It doesn't have to be private, though. In fact, as i heard it, the Singleton pattern as specified by the GoF mentions a protected constructor, for some odd reason. Something about inheritance, i hear, but singletons and inheritance do not play well together at all anyway.

    It's even possible to have a public constructor, as long as it can detect whether an instance already exists and throw an exception or something if it does. That'd be enough to guarantee singleness. But it's rather uncommon, because doing it that way complicates things by providing two apparent ways to acquire an instance -- only one of which will actually work. If you don't want outside code to be able to create a second instance, why should a constructor be part of the public interface?

    0 讨论(0)
  • 2020-12-10 08:37

    It is mandatory. Your code may be fine, but others can use this code and instantiate another object, or you might do it by accident. This way is safe.

    0 讨论(0)
  • 2020-12-10 08:39

    Singleton def:

    Ensure the only one object need to create for the entire main stack (per main class).

    If you want to satisfy above statement then we should give constructor as a private. Actually through singleton we are getting ref not object that's why it is not mandatory then we can create object in other classes but we can't access reference (Constructor as public).

    Ex:

    public class SingleTon {
    
        private static SingleTon s;
        public SingleTon() {}
    
        public static SingleTon getInstance() {
            if (s == null) {
                s = new SingleTon();
                System.out.println("ho ho");
            } else{
                return s;
            }
            return s;
        }
    }
    

    Other class:

    public class Demo {
    
        public static void main(String[] args) {
            //SingleTon s=SingleTon.getInstance();
    
            SingleTon s11=new SingleTon();
            SingleTon s12=new SingleTon();
            s11.getInstance();
            s12.getInstance();
        }
    }
    

    Output:

    ho ho
    
    0 讨论(0)
  • 2020-12-10 08:43

    A static class is different from a singleton in that a a singleton class enforces that there is always at most one instance. A static class has no instances, and is just a bundle of static functions and static data.

    So for a Singleton class, i.e. one with at most one instance, then a private constructor is required.

    In your example, it looks like the Singleton class fits more than the static class- because of the connection and dataSource members. Make those members private, your constructor private and provide static methods that reference a static ConnPoolFactory instance. If instance is null, create a new one, otherwise just use it.

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