Java.DesignPatterns.SingletonExamples

first way

package org.example;

public class SomeSingletone {

    private static SomeSingletone someSingletone;
    private SomeSingletone(){}

    static {
        synchronized (SomeSingletone.class) {
          someSingletone = new SomeSingletone();
        }
    }

    public static SomeSingletone getSomeSingletone(){
        return someSingletone;
    }

}

another way

package org.example;

public class AnotherSingletone {

    private static AnotherSingletone anotherSingletone;

    private AnotherSingletone() {
    }

    public static AnotherSingletone getAnotherSingletone() {
        if (anotherSingletone == null) {
            anotherSingletone = new AnotherSingletone();
        }

        return anotherSingletone;
    }
}
This entry was posted in Без рубрики. Bookmark the permalink.