1. design model: the most effective solution to a certain type of problem. (There are 23 General Design Patterns in java)
Singleton design mode: Only one object exists in the memory of a class.
2. The Singleton design mode can be either of the following:
1) Hungry Chinese
Initialize the object first. The class creates an object as soon as it enters the memory.
2) lazy
The object is initialized when the method is called, also called delayed loading. When the class is in the memory, the object does not exist. The object is created only when the method is called.
3. The Singleton design mode is the only object to be guaranteed:
1) in order to prevent other programs from creating such objects too much, other programs are prohibited from creating such objects first.
2) to allow other programs to access this class object, we have to customize an object in this class.
3) to facilitate access to custom objects by other programs, other access methods can be provided.
Use these three steps to implement the Code:
1) privatize Constructors
2) create a class object in the class
3) provides a method to obtain the object.
Tip: In the singleton design mode, how to describe a thing and how to describe it? When you need to ensure that the object of this thing is only in the memory, add the above three steps.
Code Description:
// 1) Hungry Chinese
[Java]
Class Single
{
Private int num;
Public void setNum (int num)
{
This. num = num;
}
Public int getNum ()
{
Return num;
}
// Privatize the constructor
Private Single (){}
// Create an object of this class in the class. Because the constructor is privatized, this class cannot create an object and wants to access it externally.
Private static Single s = new Single ();
// Provide a method to obtain the object and create an object. Therefore, you can use the following method to access the static method: (class name. method name ).
Public static Single getInstance ()
{
Return s;
}
}
Class SingleDemo
{
Public static void main (String [] args)
{
// An object cannot be created after the single-instance constructor is static, so the object is unique. Therefore, the object is accessed in the form of (class name. method name ).
Single s1 = Single. getInstance ();
Single s2 = Single. getInstance ();
S1.setNum (23 );
System. out. println (s2.getNum (); // result: 23, because the Single instance performs static and data sharing on objects and methods, the s2 Value
}
}
// 2) lazy
[Java]
Class Single
{
Private static Single s = null; // create an empty object first
Private Single (){}
Public static Single getInstance ()
{
If (s = null) // determines whether s is null. if it is null, an object is created.
S = new Single ();
Return s;
}
}
The lazy style here has security risks and solves the problem through locking, but the problem is solved and the code is increased. Therefore, we usually use the hungry style during actual development:
[Java]
Class Single
{
Private static Single s = null;
Private Single (){}
Public static Single getInstance ()
{
If (s = null)
{
Synchronized (Single. class)
{
If (s = null)
S = new Single ();
}
}
Return s;
}
}