A class has only one instance and provides a global access point to access it.
applicable places:
When a class can have only one instance and the applicable person can access it from an access point.
When this unique instance should be extensible by subclasses, and the customer should be able to use an extended instance without changing the code.
class Diagram:
Code Description:
<pre name= "code" class= "java" >/** * defines a instance operation that allows the client to access its only instance. * instance is a class operation. * Responsible for creating its own unique instance. * @author Linhai Gu * */public class Singleton {private static Singleton singleton;private Singleton () {}public static Sing Leton getinstance () {if (Null==singleton) {singleton=new singleton ();} return singleton;}}
/** * Test * @author Linhai Gu * */public class Maintest {public static void main (string[] args) {Singleton Singleton1=singl Eton.getinstance (); Singleton singleton2=singleton.getinstance (); System.out.println (Singleton1); System.out.println (Singleton2);}}
Operation Result:
Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/41413431 Emotional Control _
Create pattern--Singleton mode