Singleton mode: guarantees that the class has only one object in memory.
How do you ensure that the class has only one object in memory?
A: The construction method is private
B: Create an object yourself at the member location
C: Providing access through a public approach
a hungry man pattern of a single case: (the first to come in on the object, a home to eat, hungry.) )
1 Public classStudent {2 // A: The construction method is private 3 PrivateStudent () {4 }5 6 //create an object yourself at the member location7 //Static methods can only access static member variables, plus static8 //in order not to allow direct external access to modify this value, add private9 Private StaticStudent s =NewStudent ();Ten One // C: Provide access through a public approach A //to ensure that the outside world can use the method directly, add static - Public StaticStudent getstudent () { - returns; the } -}
Test class:
1 Public classStudentdemo {2 Public Static voidMain (string[] args) {3 //Try to create 2 objects as usual4 //Student S1 = new Student ();5 //Student s2 = new Student ();6 //System.out.println (S1 = = s2);//false, the address value is not the same, proving to be 2 different objects7 8 9 //The following is a direct change to the value of the object s, in order to avoid such a situation, so in the student class, the object is added privateTen //student.s = null; One A //How do you get the object through a single example? -Student S1 =student.getstudent (); -Student s2 =student.getstudent (); theSystem.out.println (S1 = =S2); - -System.out.println (S1);//Null,[email protected] -SYSTEM.OUT.PRINTLN (S2);//Null,[email protected] + } -}
Lazy type of Singleton mode: (Use the time to create objects, not hungry not to eat)
1 Public classTeacher {2 PrivateTeacher () {3 }4 // do not create object first, no new 5 Private StaticTeacher T =NULL;6 7 Public synchronized StaticTeacher Getteacher () {8 // to prevent thread-safety problems, use the synchronization mechanism here: Synchronized 9 // when using, make a judgment Ten if(T = =NULL) { One //if it is used for the first time, create the object At =NewTeacher (); - } - //instead of the first time, return to the object you created earlier the returnT; - } -}
Test class:
1 Public classTeacherdemo {2 Public Static voidMain (string[] args) {3Teacher T1 = Teacher.getteacher ();//T1 go in. Teacher is null, creating the object4Teacher t2 = Teacher.getteacher ();//T2 in is not null, object created with T15System.out.println (T1 = =T2);6SYSTEM.OUT.PRINTLN (t1);//[email protected]7System.out.println (T2);//[email protected]8 }9}
Single-Case mode:
A hungry man: Create an object as soon as the class is loaded
Lazy Type: When you use it, you create objects.
Interview question: What is the idea of singleton mode? Please write a code to reflect.
Development: A Hungry Man (single-mode with no problem)
Interview: Lazy (single-mode with possible problems)
A: Lazy loading (delayed loading)
B: Thread safety issues
A: Whether multithreaded environment is multi-threaded environment is
B: If there is shared data, the object T is shared
C: whether there are more than one statement operation sharing data is, T1, T2 in common operation T
Also read this article, reproduced:
A single-instance pattern of Java design Patterns
Java 23-2 design Pattern singleton mode