Phase appreciated that a Java mode: Singleton

Java design patterns generally divided into three categories

Create a schema :
the factory method pattern, abstract factory pattern, singleton, the builder pattern, prototype model.

Structural model :
Adapter mode, decorative mode, proxy mode, the appearance mode, bridge mode, combination mode, Flyweight.

Behavioral patterns :
Strategy pattern, the template method pattern, observer mode, iterator pattern, the responsibility chain mode, command mode, the memo mode state mode, the visitor pattern, intermediary model to explain the mode.

A, singleton

Singleton and starving the formula into formula lazy

Starving type:
initialized when declaring the object, a hungry man inherently thread-safe type, class loading time once the object is initialized, the efficiency is higher than the lazy man.

public class SingleTon {
    //定义私有构造方法
    private SingleTon(){
    }

    //饿汉式  类加载的时候就进行初始化
    private static final  SingleTon single = new SingleTon();

    public static SingleTon getInstance(){
        return single;
    }
}

Lazy style:
in a static initialization method, lazy type thread-safe, you need to add genlock, genlock affect the efficiency

public class SingleTon {
	//定义私有构造方法
    private SingleTon2(){


    }
    //懒汉式  双重校验保证线程安全  使用volatile可以保证指令重排序
    private volatile static SingleTon singleTon = null;


    public static SingleTon getInstance(){
        if (singleTon==null){
            synchronized (SingleTon.class){
                if (singleTon==null){
                    singleTon = new SingleTon();
                }
            }
        }
        return singleTon;
    }
}

For single-mode embodiment of understanding

  • In the single-mode embodiment only one singleton class instance, the method in which the configuration of singleton pattern must be private, to create an external object is prevented.
  • Since it is not external to instantiate, you can only create objects in this class, and must be designed to be private.
  • However, if the external access this instance of the object class to go, in this class must have a way to provide external access.
  • Singleton class may be designed as final, object is not inherited by subclasses.

Singletons starving formula:

  • For hungry man is multi-threaded style type of problem does not exist, and this is the advantage of single-case model.
  • But for hungry Chinese-style ceremony, in the time class load of static code it will be initialized, so it will create an instance of an object when the class is loaded, and allocate memory space, regardless of whether or not to use this object will occupy memory space.

Singletons lazy formula:

  • For the lazy type, although there will be created when the class is loaded object, but the object is set to null, and therefore does not occupy memory, when you call the getInstance method will be initialized instance of an object.
  • But this way there will multithreading problems, so lazy to solve the problem through the use of multi-threading volatile and synchronized key lock.

The above is a summary of singletons and understanding, please correct me exchange. White joined the line want more correction.

Published 55 original articles · won praise 6 · views 3977

Guess you like

Origin blog.csdn.net/qq_40126996/article/details/100729799