The difference between a singleton pattern in Java and Python

Source: Internet
Author: User
The examples in this article describe the differences between Java and Python in singleton mode. Share to everyone for your reference, as follows:

Single-Case mode

Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that there is only one instance of a class in the system. That is, a class has only one instance of an object.

Single-instance mode in Java

/** * Singleton mode  * Lazy Type  *  1), constructor privatization  *  2), declare private static property  *  3), provide a static method for accessing the property externally, ensure that the object exists */public class  singlecase {private static singlecase sc = Null;private singlecase () {}public static singlecase getsinglecase () {if (sc = = NULL) {return new Singlecase ();} return SC;}} /** * Simple Interest mode *  a hungry man *  1), constructor privatization *  2), declaring a private static property, creating the object at the same time  *  3), providing a static method of accessing the property externally *  */class SingleCase01 {private static SingleCase01 sc = new SingleCase01 ();p rivate SingleCase01 () {}public static SingleCase01 GetS Inglecase () {return SC;}} /*** a hungry man type * * class is loaded during use, delaying load time */class SingleCase02 {private static class innerclass{  /internal class private static SINGLECASE02 sc = new SingleCase02 ();} Private SingleCase02 () {}public static SingleCase02 getsinglecase () {return Innerclass.sc;}}

Second, the singleton pattern in Python

PS: Because Python has learned a bit long, there is no time to review, if there are errors hope that the vast number of readers point out.

Building a singleton pattern

class Test (object): __instance = None __firstinit = 1 def __new__ (CLS, *arg        S, **kwargs): if test.__instance = = None:test.__instance = object.__new__ (CLS, *args, **kwargs) Return test.__instance def __init__ (self): if not test.__firstinit:return test.__firstinit = 0if __name__ = = "__main__": A = Test () b = Test () print a print B 

in the example above, we save an instance of the class to a class property __instance, and once the class attribute is not none, we no longer call __new__, but return directly __instance. In addition, to avoid every call to Test () to perform the instance initialization, we introduce a __firstinit class property that executes the result:

<__main__. Test object at 0x000002507ff6e1d0><__main__. Test Object at 0x000002507ff6e1d0>

The same value can be proved to be the same object.


Related recommendations:

6 ways to implement a single case pattern

A summary of the single-case pattern and multiple-case patterns

Single-instance mode parsing

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