I. Singleton Model
Only one special class called singletons is included in its core structure. The singleton pattern ensures that there is only one instance of a class applying the pattern in the system. That is, a class has only one instance of an object.
Two, classification
There are two types: lazy and hungry.
III. Application scenarios
1. Objects that need to be instantiated frequently and then destroyed.
2. Objects that are too time-consuming or resource-consuming to create, but often used.
3. Stateful tool class objects.
4. Objects that frequently access databases or files.
IV: Code Implementation
1. hungry man
public class Person { /* 1.Use final modification to ensure that references are immutable and unique. 2.The Advantages and Disadvantages of Hungry Chinese Style Advantage (1)Thread safety (2)A static object has been created while the class is loaded, and the response speed is fast when the class is called. shortcoming Resource efficiency is not high, and getInstance() may never be executed. But if you execute other static methods of the class or load the class (class.forName), then the instance is still initialized. */ public static final Person person = new Person(); private String name; /** * The singleton pattern constructor should be privatized to prevent external creation */ private Person() { } public static Person getInstance() { return person; } }public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Additional Test Method
public class SingletonTest { public static void main( String[] args ) { Person person1 = Person.getInstance(); Person person2 = Person.getInstance(); Person person3 = Person.getInstance(); Person person4 = Person.getInstance(); person1.setName("zhang"); person2.setName("wang"); person3.setName("li"); person4.setName("zhao"); System.out.println(person1.getName()); System.out.println(person2.getName()); System.out.println(person3.getName()); System.out.println(person4.getName()); } }
Output:
Connected to the target VM, address: '127.0.0.1:52622', transport: 'socket' zhao zhao zhao zhao Disconnected from the target VM, address: '127.0.0.1:52622', transport: 'socket' Process finished with exit code 0
2. slacker
public class LazyPerson { private static LazyPerson lazyPerson; private String name; /** * Privatization of constructors */ private LazyPerson(){ } /** * Advantage: * It avoids the hungry Chinese style of creating instances without using them, and has high resource utilization. Without getInstance(), instances will not be created, and other static methods of this kind can be executed. * Disadvantages: * The first load was not very responsive and occasionally failed due to some reasons in the java Memory Model * @return */ public static LazyPerson getInstance(){ if(lazyPerson==null){ //Lock objects synchronized (LazyPerson.class){ if (lazyPerson==null){ lazyPerson=new LazyPerson(); } } } return lazyPerson; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
test method
public class SingletonTest { public static void main( String[] args ) { // Person person1 = Person.getInstance(); // Person person2 = Person.getInstance(); // Person person3 = Person.getInstance(); // Person person4 = Person.getInstance(); LazyPerson person1 = LazyPerson.getInstance(); LazyPerson person2 = LazyPerson.getInstance(); LazyPerson person3 = LazyPerson.getInstance(); LazyPerson person4 = LazyPerson.getInstance(); person1.setName("zhang"); person2.setName("wang"); person3.setName("li"); person4.setName("zhao"); System.out.println(person1.getName()); System.out.println(person2.getName()); System.out.println(person3.getName()); System.out.println(person4.getName()); } }
results of enforcement
Connected to the target VM, address: '127.0.0.1:53066', transport: 'socket' Disconnected from the target VM, address: '127.0.0.1:53066', transport: 'socket' zhao zhao zhao zhao Process finished with exit code 0