Concept:
In Java, the singleton pattern is a common design pattern, and the singleton pattern is divided into three kinds: lazy type single case, a hungry man type single case, registration type single case three kinds.
There are a few features of the Singleton model:
1, the Singleton class can have only one instance.
2. The Singleton class must create its own unique instance itself.
3. The Singleton class must provide this instance to all other objects.
Singleton mode ensures that a class has only one instance, and instantiates itself and provides this instance to the entire system. In a computer system, the driver objects of the thread pool, cache, log Object, dialog box, printer, and video card are often designed as singleton. These apps have more or less the functionality of the resource manager. Each computer can have several printers, but only one printer Spooler to prevent both print jobs from being output to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to prevent a communication port from being called simultaneously by two requests. In short, the selection of Singleton mode is to avoid inconsistent state, to avoid long-running political.
Public classTeststream {String name=NULL; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PrivateTeststream () {}Private StaticTeststream Ts1 =NULL; Public StaticTeststream gettest () {if(Ts1 = =NULL) {Ts1=NewTeststream (); } returnTs1; } Public voidPrintinfo () {System.out.println ("The name is" +name); } }
Public classTmain { Public Static voidMain (string[] args) {Teststream ts1=teststream.gettest (); Ts1.setname ("Jason"); Teststream ts2=teststream.gettest (); Ts2.setname ("0539"); Ts1.printinfo (); Ts2.printinfo (); if(Ts1 = =ts2) {System.out.println ("Create the same instance"); }Else{System.out.println ("Not created the same instance"); } } }
Public classEagersingleton {Private StaticEagersingleton instance =NewEagersingleton (); /*** Private default construction child*/ PrivateEagersingleton () {}/*** Static Factory method*/ Public StaticEagersingleton getinstance () {returninstance; }}
In the above example, when the class is loaded, the static variable instance is initialized, and the private constructor of the class is called. In this case, the only instance of the Singleton class is created.
A hungry man type is actually a kind of comparative image appellation. Now that you are hungry, when you create an object instance, you are more anxious and hungry, so you create an object instance when you load the class.
New Eagersingleton ();
a hungry man-type is a typical space change time , when the class is loaded will create an instance of the class, whether you do not use, first created, and then each time the call, there is no need to judge, save the running time.
Public classLazysingleton {Private StaticLazysingleton instance =NULL; /*** Private default construction child*/ PrivateLazysingleton () {}/*** Static Factory method*/ Public Static synchronizedLazysingleton getinstance () {if(Instance = =NULL) {instance=NewLazysingleton (); } returninstance; }}
In the above lazy singleton class implementation, the static factory method is synchronized to handle multi-threaded environments.
The lazy type is actually a kind of comparative image appellation. Now that you're lazy, you don't have to worry about creating an object instance. Will wait until the immediate use of the object instance will be created, lazy people, always can not shirk the time to actually do the work, so when loading the object does not create an object instance.
Null
Lazy-type is a typical time to change space , that is, every time you get an instance will be judged to see if you need to create an instance, wasting judgment time. Of course, if no one ever uses it, it doesn't create an instance, saving memory space