Black Horse Programmer--a single case design pattern

Source: Internet
Author: User

-------<a href= "http://www.itheima.com" target= "blank" >android training </a> <a href= "/http/ Www.itheima.com "target=" blank ">java training </a>, look forward to communicating with you! ----------

The role of a singleton design pattern: To make a class exist in memory only one object.

Using code to implement a singleton design pattern requires three parts:

1, initialize the constructor function.

Example: Private single () {}

2. Create an object of this class in the class.

Example: private static single s= newsingle ();

3. Provides an access method that can be obtained to the object.

Example: public static single getinstance ()

{

Returns

}

The singleton design pattern is just to make sure that the objects in memory are unique, what else to describe in the class, or how to describe them.

The singleton design pattern is written in two ways:

1, a hungry man: that is, just three examples in the statement. It is characterized by initializing the object first. For example, if the single class is in memory, the object is already created. In the actual development, the

For safety reasons, it is recommended to use the A hungry man type.

The complete code is as follows:

 
    1. Class single
    2. {
    3. private Static single s=new single ();
    4. Private single () {}
    5. public Static single getinstance ()
    6. {
    7. return s;
    8. }
    9. }

2, lazy type: It is the characteristics of the object is called when the method is initialized, which is also called the object's delay loading. For example: In the following complete code, the single class enters memory, and the object has not

exists, the object is created only when the getinstance () method is called.

The complete code is as follows:

  1. Class single
  2. {
  3. private static single s=null;
  4. Private single () {}
  5. public Static single getinstance ()
  6. {
  7. if (s==null)
  8. s=new single ();
  9. return s;
  10. }
  11. }

Black Horse Programmer--a single case 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.