As an object's creation pattern, the singleton pattern ensures that a class has only one instance, and instantiates it and supplies the instance to the entire system. This class is called a single instance class.
The single example pattern has the following characteristics:
A singleton class can have only one instance.
A singleton class must create its own unique instance.
A singleton class must provide this instance to all other objects.
A typical implementation of a single instance class is as follows: The construction child private represents a subclass that cannot be inherited.
public class Singleton
{
private static Singleton m_instance = null;
private Singleton()
{
}
public static Singleton getInstance()
{
if(m_instance==null)
{
m_instance=new Singleton();
}
return m_instance;
}
}
The so-called multiple-case pattern, in fact, is the natural extension of a single case, a singleton class can only have one instance in general, but a singleton class can also be extended to allow a finite number of instances, this pattern is a multiple-case pattern. As an object creation pattern, several examples of patterns have the following characteristics:
Multiple instances of a class can have more than one instance.
Multiple classes must create, manage, and provide their own instances to the outside world.
Many cases are divided into upper-bound multiple cases and no upper-class.
A class with a higher limit has already taken the upper bound of the instance as part of the logic and built it into the interior of multiple classes. As follows:
public class Multiton
{
private static Multiton instance1=null;
private static Multiton instance2=null;
private Multiton()
{
}
public static Multiton getInstance(int whichOne)
{
if(whichOne==1)
{
if(instance1==null)
{
instance1=new Multiton ();
}
return instance1;
}
else
{
if(instance2==null)
{
instance2=new Multiton ();
}
return instance2;
}
}
}
The number of instances of multiple classes does not need to be capped, and the number of instances with no upper bounds is called no upper-case mode. Since there is no limit to the number of instances for multiple classes, this multiple-case pattern is a generalization of a singleton pattern, but it is not necessarily possible to return to a singleton class. Aggregate management is generally used for all instances.