Software design patterns learning (nine) prototype models

Singleton

Example ensures single mode systems only one instance of a class instance and the outside is easily accessible, thus facilitating control of the number of instances and save system resources. If you want the objects of a class can only exist in a system, Singleton is the best solution.


Motivation and defined mode

Some kind of system, there is only one instance is important, for example, a system can be only one window manager or file system. Therefore, an object of the system to ensure the uniqueness of a class that is only one instance is important.

Singleton ensure only one instance of a particular class, and to instantiate the instance of the entire system and to provide this class is called singleton class that provides global access method. Singleton three points: First, only one instance of a class; second, it must create an instance of its own; it must provide their own three in this example the entire system.


Pattern Analysis

Singleton object is to ensure that only one instance of the class, and provide a global access to its access point. Singleton class has a private constructor, to ensure that users can not be instantiated directly through the new keyword. In addition, the model contains a static private member variables public static factory method, the existence of the factory method is responsible for checking instances and examples of their own, and then stored in a static member variables in order to ensure that only one instance is created. Thus the implementation of the single-mode embodiment three points to note:

  1. Singleton class constructor is private
  2. A own static private member variables
  3. Provide a public static factory method
public class Singleton {

    private static Singleton instance = null;

    //私有构造方法
    private Singleton() {

    }

    //静态公有工厂方法,返回唯一实例
    public static Singleton getInstance() {

        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
public class Client {

    public static void main(String[] args) {

        Singleton s1 = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s1 == s2);
    }
}

Here Insert Picture Description

Examples starving single class and lazy formula singleton

In the definition of static variables when instantiated singleton class, when the class is loaded singleton object has been created. When this class is loaded, static instance variables are initialized, then the private class constructor is called, the only instance of singleton class will be created.

public class EagerSingleton {

    private static final EagerSingleton instance = null;

    private EagerSingleton() {

    }

    public static EagerSingleton getInstance() {

        return instance;
    }
}

Example lazy single static class variable is not defined at the time of singleton instances, but when the factory method call the static class instance of a single embodiment, and therefore did not create a single embodiment when an object class loading.

public class LazySingleton {

    private static LazySingleton instance = null;

    private LazySingleton() {

    }

    public static LazySingleton getInstance() {

        if(instance == null) {
            instance = new LazySingleton();
        }
        return instance;
    }
}

From the point of view of resource utilization efficiency, lazy single analog embodiment of formula starving singleton class slightly better. And the reaction time from the speed point of view, starving single analog embodiment of formula lazy slightly better. However, the lazy man's singleton class at instantiation, must deal with multiple threads simultaneously for the first time when such restrictions cited access problems.

Guess you like

Origin www.cnblogs.com/Yee-Q/p/12459989.html
Recommended