Java design mode serialization (5)-single-State Mode

Source: Internet
Author: User

Java design mode serialization (5)-single-State Mode

 

Name:Singleton

 

Intent:

Ensure that a class has only one instance and provides a global access point to it.

 

Applicability:

  • When a class can only have one instance and the customer can access it from a well-known access point.
  • This unique instance should be extensible through subclass and customers should not need to changeCodeYou can use an extended instance.

     

     

    Instance:

    1. Construct a singleton king

     

    Package Singleton; public class King {Private Static king King; Private King () {} public static King getinstance () {If (King = NULL) {King = new king () ;}return King ;}}

    2. Test class

     
    Package Singleton; public class test {public static void main (string [] ARGs) {King k1 = King. getinstance (); King k2 = King. getinstance (); system. out. println (K1); system. out. println (K2); system. out. println ("whether the same:" + (k1 = k2); system. out. println ("identical:" + (k1.equals (K2 )));}}

     

     

     

  • 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.