Singleton class design - null object

My colleagues and I are discussing:

We have a singleton class that is used in several places in our code base.

The class was originally designed in such a way that you could get a class object, but this object would not be fully initialized.

By this I mean:

mySingleton.getInstance().SomeMethod();

SomeMethodwill fail because the class has not been initialized. For the class to work correctly, this had to happen:

mySingleton.getInstance().initObject();

mySingleton.getInstance().SomeMethod();

In any case, I have a discussion about what the constructor (called with the first instance of the instance) should call initObjectso that there are no errors.

What do you think?

My colleague loves it differently, so he knows when the class is initialized. (i.e., he names initObjecta specific line of code by 1 and hopes that nothing is needed first).

+5
6

Java, . , Wikipedia. 3 Java-:

public class Singleton {
    private static final Singleton INSTANCE = new Singleton();

    // Private constructor prevents instantiation from other classes
    private Singleton() {}

    public static Singleton getInstance() {
        return INSTANCE;
    }
}

" "

public class Singleton {
    // Private constructor prevents instantiation from other classes
    private Singleton() {}

    /**
     * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
     * or the first access to SingletonHolder.INSTANCE, not before.
     */
    private static class SingletonHolder { 
        public static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}

public class Singleton {
    // volatile is needed so that multiple threads can reconcile the instance
    // semantics for volatile changed in Java 5.
    private volatile static Singleton singleton;

    private Singleton() {}

    // synchronized keyword has been removed from here
    public static Singleton getSingleton() {
        // needed because once there is singleton available no need to acquire
        // monitor again & again as it is costly
        if (singleton == null) {
            synchronized (Singleton.class) {
                // this is needed if two threads are waiting at the monitor at the
                // time when singleton was getting instantiated
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}

initObject() (, ). , , initObject(), ...

, " ", 3 .

Esko, , , . , 1) singleton , 3 ; 2) , - ; 3) , . , , Java, :

public enum Singleton {
    INSTANCE;
    Singleton() {
        /* Your init code goes here */
    }
}
+8

-, initObject() , , . , , - , , , getInstance(), . , - initObject(), . - , , , ?

+1

getInstance():

public static MySingleton getInstance() {
  if (! isInitialized)
    initialize();
  return instance;
}

... initialize() .

: -, . , initObject() . , , , initObject() - , , :

// Attention everyone!! Object is initialized here!!1!11!
MySingleton instance = MySingleton.getInstance();

// Object is NOT initialized here!!11!1!!
MySingleton instance2 = MySingleton.getInstance();
+1

, .

() Singleton. - , initObject , .

0

, initObject. , . .

0

, initObject.

:

  • , .

  • , ; , .

If you need the init method, it should throw an exception if it is called twice. It may also be a good idea for a method to getInstance()throw an exception if it is called too early.

They should apply to the "traditional" exam in @rsenna's answer.

0
source

All Articles