A hungry man-type singleton and lazy-type singleton class in design mode

Source: Internet
Author: User

The characteristics of the singleton mode are:

* The Singleton class has only one instance.

* The Singleton class must create its own unique instance.

* The Singleton class must provide this instance to all other objects.

A hungry man-type singleton class:

The A hungry man Singleton class is the simplest singleton class in the Java language, which is an implementation of the class diagram that describes the A hungry man type singleton class.

In this class diagram, this class already instantiates itself.

The source code is:

Package Singleton;public class Eagersingleton {private static Eagersingleton instance=new Eagersingleton ();/** * Private default Construction Child * @return */public Eagersingleton () {}/** * static Factory method * @return */public static Eagersingleton getinstance () {return I Nstance;}}
It can be seen that when this class is loaded, the static variable instance has been instantiated, and the private constructor method of the class is called, at this point,

The Singleton class's unique instance object is created.

One of the most important features in a singleton class is that the constructor method of the class is private, and therefore the class cannot be inherited.

Lazy single-Case class:

The same as the A Hungry man Singleton class is that the constructor method of the class is private, and unlike the A Hungry man Singleton class, a lazy singleton class that instantiates itself the first time it is referenced,

As shown in the UML class diagram, a typical lazy singleton class implementation is given.

Lazy type of source code is:

The package Singleton;public class Lazysingleton {private static Lazysingleton instance=null;/** * Private constructor to ensure that the outside world cannot instantiate */ Private Lazysingleton () {}/** * static Factory method, returns a unique instance of this class */public synchronized static Lazysingleton getinstance () {if (instance== NULL) {instance=new Lazysingleton ();} return instance;}}

A hungry man singleton class and lazy type Singleton class, the A hungry man type Singleton class will instantiate itself when it is loaded, even if the loader is static,

When the A hungry man type is loaded, it will still instantiate itself, which is slightly worse than the lazy singleton class in terms of resource utilization efficiency.

But from the angle of speed and reaction time, it is slightly better than the lazy one, however, the lazy Singleton class must be handled well when instantiated.

The problem of access restrictions when multiple threads first refer to this class at the same time, especially when the Singleton class as a resource controller inevitably involves the initialization of resources,

Resource initialization can be time-consuming, which means that there is a greater chance of having multiple threads referencing this class at the same time.

A hungry man-type singleton and lazy-type singleton class in design mode

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.