A single case model of Java pattern Design (IV.)

Source: Internet
Author: User

Incomplete single case class

What is an incomplete single case class

It is estimated that some readers have seen the following "incomplete" single case class.

Code Listing 10: "Incomplete" single case class

package com.javapatterns.singleton.demos;
public class LazySingleton
{
private static LazySingleton
m_instance = null;
/**
* 公开的构造子,外界可以直接实例化
*/
public LazySingleton() { }
/**
* 静态工厂方法
* @return 返还LazySingleton 类的惟一实例
*/
synchronized public static
LazySingleton getInstance()
{
if (m_instance == null)
{
m_instance = new LazySingleton();
}
return m_instance;
}
}

The code above appears to be a "lazy" type of single class, looking closely at it, and finds a public constructor. Since the outside world can use the constructor to create any number of instances of this class, which violates the fact that the Singleton class can only have one (or a finite) instance of the attribute, the class is not a complete singleton class. This happens sometimes, like javax.swing.TimerQueue, for example, see the "Observer mode and Swing Timer" chapter in the Java and Schema book.

This can occur because of the following possibilities:

(1) Beginner's mistakes. Many beginners do not realize that a single instance of the constructor of a class cannot be made public, and thus commit this error. Some beginners of the Java language do not even know that a Java class constructor can be not public. In this case, the designer may be using self-discipline, that is, not to invoke the constructor, to use this incomplete single instance class as a single instance class in use.

In this case, a simple remedy is to change the exposed constructor to a private constructor.

(2) The design of a class as a singleton class was later found to have more than one instance. To make up for the error, simply change the constructor to public so that when more than one instance is needed, the constructor can be called at any time to create a new instance. To rectify the situation, it would be more difficult to make an improved decision on the basis of the specific circumstances. If a class was originally designed to be a singleton class, but it was later discovered that there should be more than one instance of such a class, it should be considered whether to change the singleton class to a multiple-case class (Multiton).

(3) Designer Java knowledge is good, but also know the correct use of the single example mode, but still intentionally use this incomplete single case mode, because he is intended to use a "modified" single case mode. At this point, the class must be a good single pattern, except that the common constructors do not conform to the requirements of the single case pattern.

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.