Python object-oriented singleton pattern

Source: Internet
Author: User

The singleton pattern (Singleton pattern) is a commonly used software design pattern that is primarily intended to ensure that only one instance of a class exists. A singleton can come in handy when you want the entire system to have only one instance of a class. Singleton class: If a class, from beginning to end, only opens up a piece of space belonging to the object, then this class is a singleton class. Preparatory work:----#The __init__ () method is called the initialization method----#The __new__ () method is called the construction methodExample:<1>classA:def __new__(CLS, *args, * *Kwargs):Print('New')    def __init__(self):Print('Init') A=A () Result: New<2>classA:def __new__(CLS, *args, * *Kwargs):Print('New') obj= Super ().__new__(CLS)returnobjdef __init__(self):Print('Init') A=A () Result: New Init needs to know that when we instantiate an object, there are three steps:1. Open up a space for the object2. Pass the object's space to self and execute the Init method3The space of this object is returned to the caller. The first step is to create a space for the constructor__new__() to complete. The second step is to pass the object space to self, from the example<1>As you can see, self is not received without returning space, so we give an example<2>so the second step is executed. In the third step, we return the object's space to the caller. __new__when does the () method execute? Answer: After instantiation,__init__() before executing the__new__() to create a space to understand these, we begin to write a singleton class: A singleton class: If a class, from beginning to end, only opens up a piece of space belonging to the object, then this class is a singleton class. Example:<1>classSingle :__instance=Nonedef __new__(CLS, *args, * *Kwargs):if  notAl1.__instance: CLS.__instance= Super ().__new__(CLS)returnCls.__instanceS0=Single () S1=Single ()Print(S0)Print(S1) Results:<__main__. Single Object at 0x10e7f0630> <__main__. Single object at 0x10e7f0630><2>classFoo:__instance=None @classmethoddefget_instance (CLS):ifCls.__instance:            returnCls.__instance        Else: CLS.__instance=Foo ()returnCls.__instanceF0=Foo () F1=Foo ()Print(F0.get_instance ())Print(F1.get_instance ()) Results:<__main__. Foo Object at 0x10f4354e0> <__main__. Foo Object at 0x10f4354e0>

Python object-oriented singleton pattern

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.