Java Basic Knowledge Hardening 13:java single case model use

Source: Internet
Author: User

1. Historically, emperors had only one person in history. In order to ensure their uniqueness, the ancients used to increase the "anti-counterfeiting logo" approach, such as the seal. The simpler way is to limit the creation of the emperor. In this case, the singleton pattern is used to guarantee the uniqueness of the emperor. The instance runs as follows:
Code:

(1)Empreror.java: * (Definition of the Emperor Class)

1  Packagehimi.only;2 3  Public classEmpreror {4     Private StaticEmpreror Empreror =NULL;5     PrivateEmpreror () {6         7     }8      Public Staticempreror getinstance () {9         if(Empreror = =NULL) {TenEmpreror =Newempreror (); One         } A         returnEmpreror; -     } -      the      Public voidGetName () { -System.out.println ("The first emperor in Chinese history: Qin Shihuang won the Government"); -     } -  +}

(2) Define a testing class test:

 Package himi.only;  Public class Test {    publicstaticvoid  main (string[] args) {        Empreror.getinstance (). GetName ();    }}

2. Operation result :

Java Basic Knowledge Hardening 13:java single case model use

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.