Introduction to the Singleton pattern
The singleton mode (Singleton), as its name implies, is an object-creation pattern that guarantees that a class has only one instance and provides a global access point to access it . To do this, we need to make sure that there is only one instance of a class and that this instance is easy for us to access, a global variable that allows an object to be accessed, but he cannot prevent you from instantiating multiple objects. Use the following global objects as follows:
Dog class:
public class dog{private String name; Public Dog (String name) { System.out.println ("Dog's name is" +name);} }
The object that declares the global variable in another piece of code, dog:
Public Dog dog = new Dog ("Peter");
This does not guarantee that there is only one dog.
a better approach is to have the class itself responsible for preserving its only instance. This class ensures that no other instance can be created, and it provides a way to access the instance. This is the singleton pattern.Applicability of a singleton model
The singleton mode can be applied under the following conditions:
when the class has only one instance and the customer can access it from a well-known access point;
When this unique instance should be extensible by subclasses, and the customer should be able to apply an extended instance without changing the code.
the design method of single case model
The singleton pattern requires that the constructor of the class be private , that is, the private modifier, to ensure that the object of this class cannot be instantiated by the outside world.
and singleton mode requires the class to create its own static object, and it needs to define a getinstance () method , which allows the client to access its only instance.
Classification of single-case patterns
Singleton mode is divided into a hungry man mode and lazy mode . a hungry man mode refers to the automatic creation of objects at compile time, which is called directly at runtime, which has the advantage of slow loading, fast running, and thread safety. The lazy pattern is that the object is not created at compile time and is created only when the customer first accesses it. The advantage of this method of creation is that it is faster to load, slower to run, and does not have thread safety.
sample code for a singleton pattern
First, let's write a generic a Hungry man mode program template:
Package com.singleton.hangry;/** * Design mode single case mode * A hungry man mode * load slow, run fast, thread safe * @author ZZW * */public class Hangrypattern {private H Angrypattern () {System.out.println ("singleton mode A hungry man mode" + "\ r \ n" + "features: slow loading, fast running, thread safety");} private static Hangrypattern Hangrypattern = new Hangrypattern ();p ublic static Hangrypattern getinstance () {return hangry Pattern;}}
As we can see, only the getinstance () method in the above code is public static, that is, the customer can only call this method, where we do not care about the specific implementation details of the Singleton, we will then write a customer test class:
Package Com.singleton.hangry;public class Test {public static void main (string[] args) {Hangrypattern Hangrypattern = Hang Rypattern.getinstance ();}}
again, let's write a generic lazy Mode program Template:
Package com.singleton.lazy;/** * Design mode singleton mode * Lazy mode * Load fast, slow running, thread insecure * @author ZZW * */public class Lazypattern {private lazy Pattern () {System.out.println ("lazy mode of Singleton mode" + "\ r \ n" + "features: fast loading, slow running, insecure threads");} private static Lazypattern lazypattern;public static Lazypattern getinstance () {if (Lazypattern = = null) {Lazypattern = new Lazypattern ();} return lazypattern;}}
As we can see, only the getinstance () method in the above code is public static, that is, the client can only call this method, and the lazy mode is only created when the customer first invokes it. Here we are for the sake of universality, not to care about the specific implementation details of the singleton, then we will write a customer test class:
Package Com.singleton.lazy;public class Test {public static void main (string[] args) {Lazypattern Lazypattern = Lazypatter N.getinstance ();}}
This allows us to complete the template code for the A Hungry man mode and the lazy mode, and according to the implementation details of the specific project, we can apply the template code to the application of the singleton design pattern.Advantages of a singleton design pattern
Finally, let's summarize the advantages of a singleton design pattern:
can be controlled access to unique instances;
The ability to reduce the number of variable declarations, which is a comparison of the storage of unique instances of global variables derived;
allowing fine-grained operations and representations, the Singleton class can have subclasses, and it is easy to configure an application with an instance of the extension class, and in the same way you can control the number of instances used by your app. Only operations that allow access to singleton instances need to be changed.
A single-instance pattern of Java design patterns