The concept of java singleton design pattern
This article mainly explains the concept of java singleton design pattern. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the concept of java singleton design pattern.
Overview of singleton design patterns:
The singleton mode is to ensure that the class has only one object in memory, and that the instance must be created automatically and provided externally.
The singleton model has the following characteristics:
1. A singleton class can only have one instance.
2. The singleton class must create its own unique instance.
3. The singleton class must provide this instance to all other objects.
There are three singleton modes:
Single case of lazy Chinese style
A single case of hungry Han style
Registration singleton
The singleton pattern ensures that there is only one instance of a class and instantiates itself and provides that instance to the entire system. In computer systems, thread pools, caches, log objects, dialog boxes, printers, and graphics card driver objects are often designed as singletons. These applications all have the function of resource manager more or less. Each computer can have several printers, but only one Printer Spooler to prevent two 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 one communication port from being called by two requests at the same time.
The single case code of hungry Han style is as follows:
Public class Student {
/ / construct private
Private Student () {
}
/ / make one yourself
/ / static methods can only access static member variables, plus static
/ / in order to prevent the outside world from directly accessing and modifying this value, add private
Private static Student s = new Student ()
/ / provide public access mode
/ / in order to ensure that the outside world can use this method directly, add static
Public static Student getStudent () {
Return s
}
}
The hungry Han style has created a static object for the system to use at the same time as the class is created, which will not be changed in the future, so it is inherently thread-safe.
The lazy code is as follows:
Public class Teacher {
Private Teacher () {
}
Private static Teacher t = null
Public synchronized static Teacher getTeacher () {
/ / t1,t2,t3
If (t = = null) {
/ / t1,t2,t3
T = new Teacher ()
}
Return t
}
}
Lazy singletons are not thread safe, so we need to add synchronized to the getTeacher () method to ensure thread safety.
Thank you for reading, the above is the content of "the concept of java singleton design pattern". After the study of this article, I believe you have a deeper understanding of the concept of java singleton design pattern, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!