Create pattern--Singleton mode

Source: Internet
Author: User

A class has only one instance and provides a global access point to access it. applicable places: When a class can have only one instance and the applicable person can access it from an access point. When this unique instance should be extensible by subclasses, and the customer should be able to use an extended instance without changing the code.

class Diagram:
Code Description:
<pre name= "code" class= "java" >/** * defines a instance operation that allows the client to access its only instance. * instance is a class operation. * Responsible for creating its own unique instance. * @author Linhai Gu * */public class Singleton {private static Singleton singleton;private Singleton () {}public static Sing Leton getinstance () {if (Null==singleton) {singleton=new singleton ();} return singleton;}}


/** * Test * @author Linhai Gu * */public class Maintest {public static void main (string[] args) {Singleton Singleton1=singl Eton.getinstance (); Singleton singleton2=singleton.getinstance (); System.out.println (Singleton1); System.out.println (Singleton2);}}

Operation Result:



Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/41413431 Emotional Control _





Create pattern--Singleton mode

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.