Java Singleton mode--------lazy and a hungry man-type

Source: Internet
Author: User

Single-piece mode use:
A single-piece mode is a special case of Factory mode, except that it does not require input parameters and always returns a reference to the same object.
A single-piece mode guarantees the uniqueness of a type of object in the system, that is, there is only one instance of a class in the system. Its use is very extensive, for example, we developed a simple message board, the user each message to the message will be written to the database, the most intuitive method is not written to create a database link. This is an easy way to do this, and it's a good choice when you don't think about concurrency. But in fact, a Web site is concurrent, and there may be a lot of concurrent operations. If we create a database connection for each write, the system will be vulnerable to bottlenecks and the system will be much more focused on maintenance links than on direct query operations. This is obviously undesirable.
If we can guarantee that there is only one database connection object in the system from beginning to the other, obviously we will save a lot of memory overhead and CPU utilization. This is the purpose of the single-piece mode. Of course, the single-piece mode is not just for this situation. The applicability of the single-piece mode is described in the book "Design mode: The basis of reusable object-oriented software" as follows:
1. When a class can have only one instance and the customer can access it from a well-known access point.
2, when this unique instance should be extensible by subclasses, and the customer should be able to use an extended instance without changing the code.

The following is a brief introduction to the lazy and a hungry man type of single-piece mode:
1, a hungry man: When the program starts or the single-piece mode class is loaded, a single-piece mode instance has been created.
2, lazy-type: When the program first access to a single-piece mode of the actual example is created.
How to choose: If a single-piece mode instance is often used in the system, a hungry man is a good choice.
Conversely, if the single-piece mode is seldom used or rarely used in the system, then the lazy is a good choice.

a hungry Man mode Demo:

 Public Simple () {   privatestatic single a=new single ();            Private Single () {            }      publicstatic simple getsimple () {          return  s;    }       return s;   }    

Commonly used for enumeration methods:

 enum   single;  private   single () { }  public  void   print () {System.out.println ( "Hello World" ); }}  public  class   Singledemo { public  static  void   main (string[] args) {Single A  = Si Ngle.        Single;    A.print (); }
Lazy Mode Demo:
class single{    privatestaticnull;      Public Single () {    }        ifnull)        new single ();
return s;
}
}

Lazy mode in use, easy to cause the problem of different steps, so should create a synchronous "lock", the demo is as follows

classSingle1 {Private StaticSingle1 s =NULL;  PublicSingle1 () {}//demo of the synchronization function     Public Static synchronizedSingle1 getinstance () {if(s = =NULL) s=NewSingle1 (); returns; }    //Synchronous Code Fast demo plus lock, safe and efficient     Public StaticSingle1 Getinstanceblock () {if(s==NULL)            synchronized(Single1.class) {                if(s==NULL) s=NewSingle1 (); }                returns; }}

Java Singleton mode--------lazy and a hungry man-type

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.