Java single-Instance design pattern

Source: Internet
Author: User

package com.fish.object;/*  single-case design pattern: Ensure that a class has only one object in memory.   Mode: Mode is the fixed step   to solve   a kind of   problem. The concept of   mode originated from the construction industry ....  the steps of building a house are the same.:  the foundation----->  pouring pillars-------> cover---------> Drywall--------- > Capping----> Decoration-----Stay   23 Design patterns in the software industry:     single case design mode      template design mode       Decorator Design Patterns      Observer design Patterns      Factory Design Patterns   Single-case design mode steps:  A hungry man single case design mode      1.  privatization constructor.      2.  declares a reference type variable of this class, and uses that variable to point to this class of object.      3.  provides a public static way to get objects of this class.        Lazy Single-case design mode:     1.  privatization constructor.      2.  declares a reference type variable for this class, but does not create an object,     3.  provides public static   method to get the object of this class  , get the first to determine whether you have created this class   object     &nbsp, if you have already created, then directly return the object, if not created, then first create the object of this class,       then back again.   recommends using:  a hungry man single-case design mode.    because the lazy singleton design pattern will exist threadSecurity issues, there is no guarantee that a class has only one object in memory.  */ //a hungry man single case design mode  ---->  Ensure that there is only one object in memory. class single {    //  declares a reference type variable of this class and uses it to point to this class of objects      Private static single s = new single ();    //  privatization constructor     private single ()  {    }    //  Provides a public static way to get objects of this class     public static single getinstance ()  {         return s;    }}//  Lazy Single example design mode  ---->   guarantees that single has only one object in memory. class single2 {    //  declares a reference type variable of this class and does not create an object of this class     private  static Single2 s;    //  privatized the constructors     private  Single2 ()  {    }    //    public static  single2 gEtinstance ()  {        if  (s == null)  {             s = new single2 ();         }        return s;      }}class demo5 {    public static void  main (String[] args)  {        single2 s1 =  single2.getinstance ();        single2 s2 =  Single2.getinstance ();         system.out.println ("is that the same object? " +  (S1 == S2));     }}

This article from "Small Fish Blog" blog, declined reprint!

Java single-Instance design 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.