Singleton mode (Java) and mode (Java)
// Singleton mode: public class Singleton {private static Singleton s; private Singleton () {} public static Singleton getSingleton () {if (s = null) {s = new Singleton ();} return s;} // test public static void main (String [] args) {Singleton s = Singleton. getSingleton (); Singleton s2 = Singleton. getSingleton (); System. out. println (s = s2 );}}
(I will copy the advantages and disadvantages of the singleton mode !)
The Singleton model is a design model with clear objectives, simple structure, and easy understanding. It is widely used in software development and is widely used in many applications and frameworks.
1. Main advantages
The Singleton mode has the following advantages:
(1) Singleton mode provides controlled access to a unique instance. Because the singleton class encapsulates its unique instance, it can strictly control how and when the customer accesses it.
(2) because only one object exists in the system memory, system resources can be saved. The single-instance mode of some objects that require frequent creation and destruction can undoubtedly improve the system performance.
(3) Allow variable target instances. Based on the singleton mode, we can expand and use methods similar to the singleton control to obtain a specified number of object instances, saving system resources, it also solves the problem that the single-instance objects share too much lossy performance.
2. Main disadvantages
The main disadvantages of the singleton mode are as follows:
(1) because the singleton mode does not have an abstract layer, it is very difficult to expand the singleton class.
(2) the responsibilities of the singleton category are too heavy, which violates the "single Responsibility Principle" to a certain extent ". Because the singleton class acts as a factory, provides factory methods, and acts as a product, including some business methods, integrate product creation with product functions.
(3) Currently, many runtime environments for object-oriented languages (such as Java and C #) provide the automatic garbage collection technology. Therefore, if the instantiated shared object is not used for a long time, the system will regard it as garbage and will automatically destroy and Recycle resources. The next exploitation will be re-instantiated, which will lead to the loss of the shared singleton object status.
3. Applicable scenarios
The Singleton mode can be considered in the following cases:
(1) The system only needs one instance object. For example, the system requires a unique serial number generator or resource manager, or you need to consider that the resource consumption is too large and only one object can be created.
(2) A single instance of the customer call class can only use one public access point. Besides this public access point, the instance cannot be accessed through other channels.
What is the JAVA Singleton mode?
Wait for two Singleton modes:
Hungry Chinese Style
Class Singleton {
Private static Singleton instance = new Singleton ();
Private Singleton (){}
Static Singleton getInstance (){
Return instance;
}
}
Lazy
Class Singleton {
Private static Singleton instance = null;
Private Singleton (){}
Static Singleton getInstance (){
If (instance = null)
Instance = new Singleton ();
Return instance;
}
}
What is the JAVA Singleton mode?
Wait for two Singleton modes:
Hungry Chinese Style
Class Singleton {
Private static Singleton instance = new Singleton ();
Private Singleton (){}
Static Singleton getInstance (){
Return instance;
}
}
Lazy
Class Singleton {
Private static Singleton instance = null;
Private Singleton (){}
Static Singleton getInstance (){
If (instance = null)
Instance = new Singleton ();
Return instance;
}
}