Single Case design mode

Source: Internet
Author: User

The single-instance design pattern is one of the first design patterns I have learned and one of the most familiar design patterns.

The requirement for a singleton design pattern is to produce the same object. That is, when taking the object of this class, how to take it is that one.

One of the invariant modes of the singleton pattern is the privatization of the construction method and the provision of a static class object.

The single-case design pattern is divided into two kinds, one is the lazy type, the other is the A hungry man type.

The so-called lazy is actually when the object is obtained instantiation, first determine whether the static object in the Singleton class is NULL, if it is null, then the new one, if not NULL, then return that object.

Lazy Type class Lazyer {private static Lazyer lazyer;private Lazyer () {}public static Lazyer getinstance () {if (Lazyer = = null) {Lazyer = new Lazyer (); return lazyer;} else {return lazyer;}}} public class Lazyman {public static void main (string[] args) {lazyer la = lazyer.getinstance (); System.out.println (LA); Lazyer LA2 = Lazyer.getinstance (); System.out.println (LA2);}}

The most critical difference between the so-called a hungry man and the lazy type is that the A hungry man does not have to judge whether the static object in the static singleton class is NULL, but rather the object is instantiated when the static object is defined directly.

A hungry man type class Hunger{private static Hunger Hunger = new Hunger ();p rivate Hunger () {}public static Hunger getinstance () {return Hunger;}} public class Hungryman {public static void main (string[] args) {Hunger hu = hunger.getinstance (); Hunger hu2 = Hunger.getinstance (); System.out.println (HU); System.out.println (HU2);}}

Difference: Because the lazy-type more judgment is empty this step, so in multi-threaded time there will be concurrency problems occur. The A hungry man does not have this step and is relatively efficient and does not have to consider concurrency issues.


========================================================================

Today, the state is not good, it was intended to strengthen the study of understanding every day, today write a simple thing to put.

Single Case 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.