Design Pattern--Singleton (Singleton) example

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ></span>

The English original of the singleton mode is: Ensure a class has only one instance, and provide a global point of access to it. This means ensuring that a class has an instance and provides this instance to the entire system. The singleton mode is primarily to ensure that only one instance exists. There are two forms of representation in the Java language:
A hungry man singleton: classes are instantiated when they are loaded.
Package com.zz.singleton;/** * A hungry man single case * @author TXXS * */public class Hungrysingleton {//class is instantiated at load time private static hungrysing Leton m_instance = new Hungrysingleton ();//construction privatization to ensure that the outside world cannot directly instantiate private Hungrysingleton () {}//Gets the instance object public static by the following methods Hungrysingleton getinstance () {return m_instance;}}

Lazy Singleton: The object is instantiated only when the class is referenced for the first time.

Package com.zz.singleton;/** * Lazy Singleton * @author TXXS * */public class Lazysingleton {//The first time a class is referenced, object instantiation private static Lazysi Ngleton m_instance = null;//privatization of the constructor display ensures that the private Lazysingleton () {}//method synchronization cannot be instantiated directly by the outside world, preventing simultaneous access of two threads to m_ instancesynchronized public static Lazysingleton getinstance () {if (m_instance = = null) {m_instance = new Lazysingleton ();} return m_instance;}}

Advantages of Singleton mode:

1, only one instance, reduce memory expenditure.

2, reduce the performance cost of the system.

3. You can avoid simultaneous operation of the same resource.

Disadvantages of the Singleton pattern:

Unable to create subclass, expansion difficulty.

Haha, I wrote a code, reference to the great God, learn from each other progress, download

Design Pattern--Singleton (Singleton) example

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.