Singleton mode of design mode
Definition: Ensure a class has only one instance, and provide a global point of access to it Ensure that a class has only one instance, instantiates itself and provides access to the entire system.
advantage:
- Singleton mode has only one instance in memory, which reduces memory expenditure and system performance overhead. Especially when an object needs to be created and destroyed frequently.
- The singleton mode can avoid multiple occupation of resources, such as an action to write a file. Because only one instance exists, it can avoid writing to a resource file at the same time.
- The singleton mode sets global access points on the system to optimize shared access.
Disadvantages:
- Generally, there is no interface, so it is difficult to expand.
Common application scenarios: in a system, a class is required to have only one object, and multiple objects may cause adverse reactions.
- A unique serial number is required
- File IO and database access
- Site counters, requiring synchronous sharing
- Only one Task Manager of windows can be opened
- Recycle Bin for windows
Common template for singleton mode:
package com.lvshui5u.designpattern.singletonpattern; /** * @author: lvshui5u * @date: 2021/7/18 8:28 * @describe: Template for singleton mode * * Ensure a class has only one instance, and provide a global point of access to it. * Ensure that a class has only one instance, instantiates itself and provides access to the entire system. * */ public class Singleton { /** * Self instantiation */ private static final Singleton singleton = new Singleton(); /** * Generate multiple objects through private restriction */ private Singleton(){} /** * The instance object is obtained by this method * @return example */ public static Singleton getInstance(){ return singleton; } public static void doSomething(){ // do something } }
As can be seen from the code, an instance of new is created during class loading, and the instance is made unique by privatizing the constructor. Get through the getInstance() method of the class to achieve global access.
Generic template for lazy singleton mode:
Difference: the former creates a new instance when loading the class. If the instance is not used during the whole program running, it will cause a waste of resources. When the latter needs getInstance(), first judge whether an instance has been generated, and then consider whether to generate an instance. It avoids wasting resources and reduces the use efficiency (locking, releasing and judging whether it is null).
package com.lvshui5u.designpattern.singletonpattern; /** * @author: lvshui5u * @date: 2021/7/18 9:03 * @describe: Lazy single case * Problem: multiple instances may occur during concurrency (it takes time to generate instances) * Solution: synchronized * * It avoids wasting resources and reduces the use efficiency (locking, releasing and judging whether it is null) */ public class LazySingleton { private static LazySingleton LAZY_SINGLETON = null; private LazySingleton(){} public static LazySingleton getInstance(){ if(LAZY_SINGLETON == null){ LAZY_SINGLETON = new LazySingleton(); } return LAZY_SINGLETON; } }
Possible problems: in the case of high concurrency, thread A determines that the instance is null and decides to new an instance. Object instantiation takes A certain time. During this time, thread B determines that the instance is null and also goes to new an instance.
Simulation:
public class Test extends Thread { @Override public void run() { LazySingleton lazySingleton = LazySingleton.getInstance(); System.out.println(lazySingleton); } public static void main(String[] args) { for (int i = 0; i < 1000; i++) { Test test = new Test(); test.start(); } } }
result:
com.lvshui5u.designpattern.singletonpattern.LazySingleton@41ad0c3c com.lvshui5u.designpattern.singletonpattern.LazySingleton@31ba7625 com.lvshui5u.designpattern.singletonpattern.LazySingleton@41ad0c3c ......
Solution: use the synchronized keyword
Refer to Zen of design pattern