• Single Case class
Single-instance class, where the class can only create one object, guaranteeing the uniqueness of the object instance.
Example: Single-instance class
Class Singleton { private static Singleton instance; Use the static modifier, because it is accessed in the static method of the class, and the function //is a reference variable pointing to the object. Private Singleton () {}; Provides a private constructor that uses methods to control the number of objects it creates. The //method must be public and static, because the method is to be exposed by the following class call and the following S1 reference variable gets the method by //through the class. method. Because you cannot create objects, you can only use classes. This constrains the creation of only one object. Public static Singleton getinstance () { if (instance = = null) { instance = new Singleton (); } return instance; } } public class Singletontest {public static void Main (string[] args) { Singleton S1 = Singleton.getinstance (); Singleton s2 = singleton.getinstance (); System.out.println (S1 = = s2); The result is true, as you can see if the same object "= =" compares the memory addresses of two objects.
//a hungry man type
Class Single1
//{
private static Single1 S=null;
Private Single1 () {}
public static Single1 getinstance ()
// {
if (s==null)
S=new single ();
return s;
// }
//}
//Lazy Type
//principle defines a single case in principle with the A Hungry man type
Class Singledemo
{
public static void Main (String [] args)
{
System.out.println ("Hello World");
}
}
1. In order to avoid other programs to build this kind of object more. Prevent other programs from setting up this type of object first
2. The other program asked to change the object. Had to be in this class. Customizing an Object
3. To facilitate access to custom objects by other programs. can provide a number of external access methods
In order to avoid other programs to build this kind of object more. Prevent other programs from establishing this class object first.
If the constructor in the object can pass in the parameter. Build the object. The object initialization data is changed. The object only provides functionality. Avoid other calls to create objects, and change the initial data. So encapsulate the object
1. The singleton mode guarantees that a class has only a unique instance and provides a global access point.
2, the single-case mode has 3 main features:
(1) , the Singleton class ensures that you have only one instance.
(2) , the Singleton class must create its own instance.
(3) , the Singleton class must provide a unique instance for other objects.
3, for the lazy model, we can understand this: the Singleton class is very lazy, only when they need to act, never know to prepare early. It determines whether an object exists when it is needed, creates an object immediately if it does not, and then returns if an existing object is no longer created and returns immediately.
4, for the A Hungry man mode, we can understand this: the Singleton class is very hungry, the urgent need to eat, so it is loaded in the class immediately when the object is created.
Java Singleton class