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.
First, we look at a classic single-instance implementation.
Public class Singleton {
Private Static Singleton uniqueinstance = null;
Private Singleton () {
Exists only to defeat instantiation.
}
Public Static Singleton getinstance () {
if (uniqueinstance = = null) {
uniqueinstance = new Singleton ();
}
return uniqueinstance;
}
Other methods ...
}
Singleton by restricting the construction method to private to prevent the class from being instantiated externally, the unique instance of Singleton can only be accessed through the getinstance () method within the same virtual machine scope. (In fact , the Java reflection mechanism is the ability to instantiate a class constructed as private, which basically invalidates all Java Singleton implementations.) This issue is not discussed here, and it is deceiving to assume that the reflection mechanism does not exist. )
However, the above implementations do not take into account thread safety issues. Thread safety refers to the idea that if your code is in a process where multiple threads are running concurrently, these threads may run the code at the same time. If the result of each run is the same as the single-threaded run, and the value of the other variable is the same as expected, it is thread-safe. Or, the interface provided by a class or program for a thread is an atomic operation or a switchover between multiple threads does not result in ambiguity in the execution of the interface, that is, we do not have to consider the problem of synchronization. Obviously the above implementations do not meet the requirements of thread safety, and many singleton instances are likely to occur in concurrent environments.
1 public class Teststream {
2 private String name;
3 Public String GetName () {
4 return name;
5 }
6 Public void SetName (String name) {
7 this.name = name;
8
9 //The class can have only one instance
Teststream () {} //Private non-parametric construction method
One //The class must be created on its own
2 different ways
/*private static final Teststream ts=new teststream (); */
+ private static Teststream ts1=null;
//This class must automatically provide this instance object to the entire system
Public static Teststream Gettest () {
if (ts1==null) {
ts1=new Teststream ();
+ }
return ts1;
+ }
Public void GetInfo () {
System.out.println ("output message" +name);
+ }
25}
1 public class Testmain {
2 public static void Main (String [] args) {
3 teststream s=teststream.gettest ();
4 s.setname ("Zhang Xiaoxiang");
5 System.out.println (S.getname ());
6 Teststream s1=teststream.gettest ();
7 s1.setname ("Zhang Xiaoxiang");
8 System.out.println (S1.getname ());
9 s.getinfo ();
Ten s1.getinfo ();
if (s==s1) {
System.out.println ("Create the same instance");
}else if (s!=s1) {
System.out.println ("Not the same instance created");
}else{
System.out.println ("Application Error");
+ }
(+ }
19}
Operation Result:
Zhang Xiaoxiang
Zhang Xiaoxiang
Output message Zhang Xiaoxiang
Output message Zhang Xiaoxiang
Create the same instance
Conclusion: The result shows that the singleton pattern provides an object-only access point for an object-oriented application, and the entire application can enjoy an instance object regardless of the function it implements.
1. A hungry man type single case class
2 public class Singleton1 {
3 //private default constructor
4 Private Singleton1 () {}
5
6 private static final Singleton1 single = new Singleton1 ();
7
8 public static Singleton1 getinstance () {
9 return single;
Ten }
11}
2. Lazy single-case class
2 public class Singleton2 {
3 //private default constructor
4 Private Singleton2 () {}
5 //Note that there is no final
6 private static Singleton2 single=null;
7
8 public synchronized static Singleton2 getinstance () {
9 if (single = = null) {
Ten single = new Singleton2 ();
One }
return single;
- }
14}
3. Registered Singleton class
1 Import Java.util.HashMap;
2 Import Java.util.Map;
3//Registration type Singleton class.
4//Similar to the method in spring, the class name registration, the next time from the inside to get directly.
5 public class Singleton3 {
6 private static map<string,singleton3> Map = new hashmap<string,singleton3> ();
7 static{
8 Singleton3 single = new Singleton3 ();
9 Map.put (Single.getclass (). GetName (), single);
10}
11//Protection of the default construction child
Protected Singleton3 () {}
13//Static Factory method, return this kind of unique instance
public static Singleton3 getinstance (String name) {
if (name = = null) {
+ name = Singleton3.class.getName ();
System.out.println ("name = = NULL" + "--->name=" +name);
18}
if (map.get (name) = = null) {
try {
Map.put (name, (Singleton3) class.forname (name). newinstance ());
\ catch (Instantiationexception e) {
E.printstacktrace ();
(Illegalaccessexception e) {
E.printstacktrace ();
(ClassNotFoundException e) {
E.printstacktrace ();
28}
29}
return Map.get (name);
31}
32//A schematic business method
The public String is about () {
Return "Hello, I am Regsingleton.";
35}
-public static void main (string[] args) {
PNS Singleton3 single3 = singleton3.getinstance (null);
System.out.println (Single3.about ());
39}
40}
Singleton mode (RPM)