When should we go to the Singleton class in Java?

According to my thoughts, we should create the Singleton class when we use the same state of the object in the application. In this case, we want the user to be limited from creating a new instance each time so that they cannot support multiple states. Agreed. But the same behavior can be achieved by declaring instance variables as static. It seems to me that it will also serve the same purpose, whether it be a class cacheobjectcontainer, loggeror Classloader.

Please help me understand the concept above, where a static instance variable will not solve the target and the class must be declared Singleton?

Edited Part

Ok, let me bring you even more clarity. The doll of a single class is to save only one instance of a singleton class via jvm. Agreed. But I'm trying to think about the reasons why we want to keep only one instance. Two reasons are possible:

1) An object can be expensive to create. Therefore, we just want to keep only one instance. Consistent in this scenario, declaring instance variables as static does not solve any goals.

2) We want to use the same state of the object in the application. I thought this was the main purpose of declaring a class as singleton. But this can be achieved simply by declaring instance variables as static.

But it seems that 1 is the main reason for marking up any class as static, and not argument 2, because it can also be achieved using a static variable.

Is it correct?

+5
source share
4 answers

Declaring an instance variable makes the link a reference to a static object. So, for this class there is only one instance. But this does not stop anyone else from doing it, new SomeObject()regardless of whether it is a static link. The idea of ​​having a singleton class is to manage instances. For example, if you create a constructor private, you cannot do newto create a new instance. Therefore, you control the creation of instances.

+5
source

, singleton , , , . Singletons .

+4

- , , , . , , , - ? singleton , . , singleton, , .

, , , -. , .

public class SessionManager {
    private static final SessionManager instance;
    static {
        instance = SystemConfig.isDebug() ? new DebugSessionManager() : new SessionManager();
    }

    public static SessionManager getInstance() {
        return instance;
    }


    public int getActivePersonId() {
         // default implementation
    }
}

public class DebugSessionManager : SessionManager {
    @Override
    public int getActivePersonId() {
         // debug implementation
    }
}

// The code can be used in the same way regardless of whether we're in debug mode:
int personId = SessionManager.getInstance().getActivePersonId();

Update

, - :

public class SessionManager {
    private static String systemName;
    public String getSystemName() {return systemName;}
}

... , systemName , , new SessionManager().getSystemName() SessionManager.getInstance().getSystemName(). :

  • , new SessionManager(), - new. , SessionManager systemName. , , , .
  • new SessionManager(), , .

, , Singleton. : Singletons - -. .

+1

Some good answers so far.

This When article of Singleton not a Singleton describes the concept well.

Here are some additional differences:

  • Singlets can be operational, but static variables cannot.
  • Single classes can be subclasses.
+1
source

All Articles