Design patterns are reusable solutions that have been proposed by 4 authors, called "GoF", also known as quad groups.
The design pattern is divided into: creation type, structure type, behavior type
The singleton design pattern belongs to the creation type
The three elements of a singleton pattern:
1. Variables of this type that need to have static private in the class body
2. The construction method must be private
3. Provide a public static entry point method
Lazy-public class singleton{ private static singleton st;// Think about why it's static? private singleton () { } public static singleton getinstance () { if (st==null) { st = new singleton (); } return st; }}//a hungry man type public class singleton{ Private static singleton st=new singleton ();//Think about why it's static? private singleton () { } public static singleton getinstance () { return st; }}
This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1661146
A singleton design pattern in Java