Java implementation of single example (Singleton) mode [00 original]__java

Source: Internet
Author: User
This example introduces the two most common examples of single example design patterns--a hungry man single example and lazy single example.

1. Class Diagram

2. Java Implementation CodePackage Cn.edu.ynu.sei.singleton;

/**
* "A Hungry Man" type of single case class
*
* @author 88250
* @version 1.0.0, 2007-8-17
*/
public class Eagersingleton
{

/**
* Single Instance
*
* @uml. Property Name= "Instance" readonly= "true"
*/
Private static final Eagersingleton instance = new Eagersingleton ();

/**
* Private Default Builder
*/
Private Eagersingleton ()
{
}

/**
* Static Factory method
*
* @return Returns the instance.
*/
public static Eagersingleton getinstance ()
{
return null;

}
}


Package Cn.edu.ynu.sei.singleton;

/**
* "Lazy type" single case
*
* @author 88250
* @version 1.0.0, 2007-8-17
*/
public class Lazysingleton
{

/**
* Single Instance
*
* @uml. Property Name= "Instance"
*/
private static Lazysingleton instance;

/**
* Private Default Builder
*/
Private Lazysingleton ()
{
}

/**
* Static Factory method
* @return Returns the instance.
* @uml. Property Name= "Instance"
*/
public static Lazysingleton getinstance ()
{
if (instance = null)
{
Instance = new Lazysingleton ();
}
return instance;
}

}

3. Summary
This is just the simplest example, and in a single example there is a pattern called "registration", which overcomes the drawbacks of not inheriting both of these single examples. If you are interested, you can check the relevant information, no longer repeat here. Here we briefly discuss the issues that should be noted in the use of a single case pattern in distributed systems and multi-threaded environments.

In a distributed system, a singleton class that is designed may be instantiated in a different JVM, which is equivalent to having more than one instance of a singleton pattern in the global perspective. The point to note is that if the single case is stateless, then there is no problem. However, if the single case is stateful, it can create a problem. For example, if a single instance object can hold an int-type attribute to provide a system with a unique serial number, the user will see the same number several times as a billing number for a traffic system. Therefore, in any distributed system that uses such technologies as EJB, RMI, and Jini, a stateful single case pattern should be avoided.

In a multithreaded environment, you must pay attention to the use of keyword synchronized synchronization getinstance method. If you are an experienced C/s + + programmer, you may use the "double check Precedent" code mode to solve this problem, but this code-level pattern in Java is completely unable to set up. Refer to the Java and design patterns and [BLOCH01, GOETZ01, DCL01] for the "double check precedent" question.

So the abstract factory is only to some extent to support the OCP, in the design time this is worth our attention.

4. References   "Design Patterns", "Java and schema"

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.