Single-State Design Mode

Source: Internet
Author: User
The so-called single-State Design Mode of classes is to adopt a certain method to ensure that only one object instance can exist for a class in the entire software system, in addition, this class only provides one method to get its object instance. If you want a class to generate only one object in a virtual machine, you must first set the access permission of the class constructor to private, in this way, the new operator cannot be used to generate class objects outside the class, but the class objects can still be generated within the class. Because the class object cannot be obtained from outside the class, you can only call a static method of the class to return the object created inside the class. The static method can only be changed to the static member of the category, changes to the class objects generated inside the class must also be defined as static. Example public class testsingle {Private Static final testsingle onlyone = new testsingle (); public static testsingle gettestsingle () {return onlyone;} private testsingle () {}} public class schematicdialog extends jdialog {Private Static schematicdialog instance = NULL; private schematicdialog () {super (jframe. getframes () [0]); super. add (schematicmainpanel. getinstance (); // super. setsize (1000,600); dimension = toolkit. getdefatooltoolkit (). getscreensize (); // super. setlocation (INT) (dimension. getwidth ()-Super. getwidth ()/2, (INT) (dimension. getheight ()-Super. getheight ()/2); super. setsize (INT) dimension. getwidth ()-10, (INT) dimension. getheight ()-50); super. setmodal (false); super. addwindowlistener (New windowadapter () {public void windowclosing (invalid wevent e) {schematicloaddata. getinstance (). clearschematiclayers () ;}});} public void setvisible (Boolean B) {schematicmainpanel. getinstance (). getlayoutcombobox (). setselectedindex (0); super. setvisible (B);} public static schematicdiible getinstance () {If (instance = NULL) {instance = new schematicdialog ();} return instance ;}}
Related Article

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.