Single-profit mode and implementation of cache Functions

Source: Internet
Author: User
In the design pattern book, the author describes the Singleton pattern in this way: ensure that a class has only one instance and provides a global access pointer to it.
To ensure that there is only one instance globally, all the pointers to the instance accessing this class must have the same address and meet the following conditions:

  1. Private constructor;
  2. Create this example by yourself;
  3. Static instantiation provides globally unique instances.

In the singleton mode, the standalone mode can be divided into the hungry Chinese standalone mode and the lazy Chinese standalone mode. The so-called hungry Chinese Singleton Mode means that the unique instance is created when the class is accessed, regardless of whether it will be used; the lazy Singleton mode will not be instantiated when the class is loaded. It will only be instantiated when the static instantiation method is used for the first access.
Theoretically, it is easy to implement the ELE. Me Singleton mode, but it will reduce the running efficiency to a certain extent, the implementation method is somewhat complicated. The following is a sample code for the two Singleton modes:

Hungry Chinese Singleton mode public class EagerSingleton
{
Private static final EagerSingleton m_instance = new EagerSingleton ();
/** // <Summary>
/// Private default constructor
/// </Summary>
Private EagerSingleton (){}
/** // <Summary>
/// Static factory Method
/// </Summary>
Public static EagerSingleton getInstance ()
{
Return m_instance;
}
}

LazySingleton
{
Private static LazySingleton m_instance = null;

/** // <Summary>
/// Private default constructor to ensure that external entities cannot be directly instantiated
/// </Summary>
Private LazySingleton (){}

/** // <Summary>
/// Static factory Method
/// </Summary>
/// <Returns> return the unique instance of this class </returns>
Public static LazySingleton getInstance ()
{
If (m_instance = null) // 1. In multithreading mode, there may be more than two threads at the same time.
{
M_instance = new LazySingleton ();
}
Return m_instance;
}
}

Maybe you will say that using the protected or public constructor is not enough? Theoretically, this is not acceptable. If the protected constructor is used, it cannot be guaranteed that the constructor will not be instantiated again in the subclass, but the public constructor is more self-evident.
You may have seen that the lazy Singleton mode in the preceding code can only run in a single-threaded program, but it is not safe for multithreading, just as the comments in the code. If more than two threads pass judgment at the same time, more than two object instances will be generated in the following process, and the m_instance variable will eventually point to the result of the last instantiation, however, there is no guarantee that the pointer to the returned results of this instantiated function points to the same instance. That is to say, in the multi-threaded mode, the singleton mode principle has been violated. To ensure a globally unique instance, the dual-verification method can be used. The following code is used:
Public class LazySingleton
{
Private static LazySingleton m_instance = null;
Private static object synRoot = new object ();

/** // <Summary>
/// Private default constructor to ensure that external entities cannot be directly instantiated
/// </Summary>
Private LazySingleton (){}

/** // <Summary>
/// Static factory Method
/// </Summary>
/// <Returns> return the unique instance of this class </returns>
Public static LazySingleton getInstance ()
{
If (m_instance = null)
{
Lock (synRoot)
{
If (m_instance = null)
{
M_instance = new LazySingleton ();
}
}
}
Return m_instance;
}
}

For more information about the singleton mode, see the following:
Http://xxol.net/Edu/Development/Java/0541312241282488.htm
Http://xxol.net/Edu/Development/Java/0541312240656996.htm
Http://xxol.net/Edu/Development/Java/0541312240591257.htm
Http://xxol.net/Edu/Development/Java/0541312235356276.htm

The registration-type multi-sample mode is extended based on the singleton mode, and uses keywords to obtain globally unique object instances. Therefore, the implementation of simple cache functions can also be implemented using this method. The difference is that Object Instantiation is not implemented by the cache container, but created by methods other than the cache container, then obtain the instance from the cache container with the keyword, or modify and delete the instance based on the keyword. It can be seen that the functions of the cache server are more flexible than those of the Multi-sample mode during registration, rather than limited to the access of the object instance.

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.