background : Recently in the study of teacher Han's notes are not very familiar with a single case and many examples, so through the Internet to find information on the way to learn.
Design Patterns : The best practice is the software developers in the software development process faced with a general solution, that is, the experience of development summary.
Singleton Mode (Singleton): is one of the simplest design patterns in Java. This type of design pattern belongs to the Create pattern, which provides an optimal way to create an object.
This pattern involves a single class that is responsible for creating its own objects, while ensuring that only a single object is created. This class provides a way to access its only object, which can be accessed directly without instantiating an object of that class.
The characteristics of a singleton class:
- A singleton class can have only one instance.
- The Singleton class must create its own unique instance.
- The Singleton class must provide this instance to all other objects.
- The construction method of the Singleton class is private. (Avoid external use of construction methods to create multiple instances directly
Multi-case Mode (Multiton): as the creation mode of an object, multiple instances of a multi-instance pattern can have multiple examples, and multiple classes must create, manage their own instances themselves, and provide their own instances to the outside world.
the characteristics of the multi-sample class :
- Multiple cases can have more than one instance.
- A multi-instance class must create itself, manage its own instances, and provide its own instance to the outside.
- Based on whether there is an instance upper limit is divided into the upper limit of multiple cases and unlimited multi-class.
Comparison of multiple cases of a single model:
- Singleton mode and multi-case mode belong to object mode.
- Neither of them provides a constructor method, that is, the construction method is private.
- The Singleton mode object has only one copy in the whole system, and multiple instances can have multiple examples.
- Single case and multi-instance pattern structure diagram
- The singleton is that all requests are handled with an object, such as the service and DAO layer objects are usually singleton, and many examples are each request is handled with a new object, such as action;
- A singleton can save CPU and memory, and multiple cases can prevent concurrency problems, that is, one request changes the state of an object, when the object processes another request, and the state of the previous request object causes the object to handle the error of another request, and when the object contains a state that can be changed, it uses multiple cases instead of a single case.
Single-Instance implementation:
Create a Singleobject class. The Singleobject class has its own private constructor and a static instance of itself. The Singleobject class provides a static method for the outside world to obtain a static instance of it.
Packageecut.enums; Public classSingleobject {//create an object of Singleobject Private StaticSingleobject instance =NewSingleobject (); //let the constructor be private so that the class is not instantiated PrivateSingleobject () {}//get the only available objects Public StaticSingleobject getinstance () {returninstance; } Public voidShowMessage () {System.out.println ("Hello world!"); }}
Use the Singleobject class in Singletonpatterndemo to get singleobject unique objects.
Packageecut.enums; Public classSingletonpatterndemo { Public Static voidMain (string[] args) {//Illegal Constructors//compile-time error: Constructor Singleobject () is not visible//Singleobject object = new Singleobject (); //get the only available objectsSingleobject Object =singleobject.getinstance (); //displaying MessagesObject.showmessage (); }}
The results of the operation are as follows:
Hello world!
Several implementation methods of the single case:
1, lazy, thread unsafe
Lazy initialization (lazy loading, creating singleton objects when needed, rather than when the software system is running or when the class is loaded by the loader )
Java Singleton and multi-instance