Black Horse programmer--java Basic---single case

Source: Internet
Author: User

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

single Case

Design Patterns: The most effective way to solve problems, to simplify the problem, there are a few in Java .

Singleton: Solve a class in memory with only one object

To ensure that the object is unique:

1, in order to avoid other programs to establish such objects too much, first prohibit the control of other programs to establish the object.

2, and in order for its program to have access to the class object, we have to customize an object in this class.

3, in order to facilitate other programs on the custom object access, can provide some external access.

How do these three parts be represented in code?

1, privatize the constructor.

2, create an object of this class in the class.

3, which provides a way to get to the object.

Initialize the object first

called: a Hungry man type

single class into memory, you have created the object

This method is generally used in development, because the security

1 classsingle{2 3      PrivateSingle () {}4 5      Private StaticSingle S =NewSingle ()6 7     Public StaticSingle getinstance () {8 9          returns;Ten  One      } A  - } -  the   -  - classsingledemo{ -  +     Public Static voidMain (string[] args) { -  +Single SS =single.getinstance (); A  at    } -  -}

How to describe things and how to describe them.

When it is necessary to guarantee the object of this thing to be unique in memory, add the above three parts.

The second method:

The object is initialized only when the method is called, and is called a deferred load of the object.

called: Lazy type

Single class into memory, object does not exist, object is established only when the getinstance () method is called

if an object calls getinstance () There is no problem, but a problem may occur when multiple objects are called

1 classsingle{2 3PrivateStaticSingle S =NULL;4 5      PrivateSingle () {}6 7       Public StaticSnigle getinstance () {8 9If (s==NULL)Ten  Ones=NewSingle (); A  -              returns; -  the      } -  - } -  +   -  + classsingledemo{ A  at     Public Static voidMain (string[] args) { -  -Single SS =single.getinstance (); -  -    } -  in } -  tohttp//tieba.baidu.com/p/794604916 +  -   the  *Publicclasssingleton{ $ Panax Notoginseng Private Singleton () {} -  the   +  APrivateStaticfile the  +}

-----------Android Training, Java training, Java Learning Technology blog, look forward to communicating with you! ------------

Black Horse programmer--java Basic---single case

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.