Singleton patterns in Java (classes defined with singleton mode)

Source: Internet
Author: User

1 Singleton (Singleton) mode: A singleton mode ensures that a class has only one instance, and that the class can only be an object creation pattern that itself instantiates itself and exposes this instance to other classes

Class with Singleton mode: According to the singleton pattern, we know that it meets the following three points

1. Ensure that there is only one instance of a class

2. and instantiate Yourself

3. and provide this instance class to other classes.

2 Ensure that the above three points of the adopted programming strategy
* Declare the construction method as private. Make sure that you can create additional instances by yourself, by avoiding the creation of external instances or by inheriting the quilt class;
* Define a private static instance of the class as the data field for that class. Ensure that only one instance of a class is available;
* Define a static factory method. An outer class cannot instantiate an object of that class, so it can only be made available to other classes with static methods, returning only instances of this singleton class.

3, the use of single-case mode conditions:
There is a requirement for using singleton mode: A singleton pattern should be used when a system requires only one instance of a class, and conversely, if a class can have several instances coexisting, then it is not necessary to use a singleton pattern class.

4 single cases in Java in 3 (general form) Form

Active Singleton class:

public class Activesingleton {

private static final Activesingleton m_instance = new Activesingleton ();
Instantiate a own object when the class is loaded
Private Activesingleton () {
}
Each call to the factory method returns the instance
public static Activesingleton getinstance () {
return m_instance;
}
}

One of the most important features of the Singleton class in the Java language is that the construction method of the class is private, thus avoiding the external use of the constructor to create any number of instances directly. Because constructs are private, this class cannot be inherited. The active Singleton class instantiates one of its own objects when the class is loaded.

Passive Singleton class:

public class Lazysingleton {
private static Lazysingleton m_instance = null;

Private Lazysingleton () {
}
A static factory method that instantiates itself when it is first called
Synchronized public static Lazysingleton getinstance () {
if (m_instance = = null) {
M_instance = new Lazysingleton ();
}
return m_instance;
}
}

As with the active Singleton class, the construction method of the passive singleton class is private, unlike when it is instantiated for the first time, and if the loader is static, it does not instantiate itself when the passive singleton class is loaded.

Registered Singleton class:
Registered Singleton This singleton actually maintains an instance of a set of singleton classes (parent and subclass), stores these instances in a map (register), returns from the factory for an already registered instance, and, for unregistered cases, registers first and then returns.
public class Regsingleton {
private static HashMap M_registry = new HashMap ();

static {
Regsingleton x = new Regsingleton ();
M_registry.put (X.getclass (). GetName (), x);
}

Protected Regsingleton () {
}

public static Regsingleton getinstance (String name) {
if (name = = null) {
Name = "Regsingleton";
}
if (m_registry.get (name) = = null) {
try {
M_registry.put (name, Class.forName (name). newinstance ());
} catch (Exception e) {
System.out.println ("Error happened.");
}
}
Return (Regsingleton) (M_registry.get (name));
}

A test method
Public String about () {
Return "Hello, I am Regsingleton";
}
}

Subclass of a registered singleton class
public class Regsingletonchild extends Regsingleton {
Public Regsingletonchild () {
}

Static Factory method
public static Regsingletonchild getinstance () {
Return (Regsingletonchild) regsingleton.getinstance ("Regsingleton")
}
Public String about () {
Return "Hello, I am Regsingletonchild";
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.