Definition: Ensure that a class has only one instance, and instantiate itself to provide the entire instance to the entire system
scenes to be used:
- Environments that require production of unique serial numbers
- Requires a shared access point or shared data, such as a program counter, throughout the project
- Creating an object consumes too many resources
- Environments that need to define a large number of static constants or static methods
Example:
package cn.lonecloud.singleton; /** * @author lonecloud * @version v1.0 * @date 3:44 PM 2018/3/16 */ public class DemoSingleton { private static final Demo demo=new Demo(); public static final Demo getInstance(){ return demo; } } class Demo{ }