Example analysis of Python single case pattern _python

Source: Internet
Author: User

This example describes the Python singleton pattern. Share to everyone for your reference. The specific analysis is as follows:

__new__ () is called before __init__ () to generate the instance object. This method and the attributes of the class can be used to realize the single case pattern of the design pattern. A singleton pattern is the creation of a unique object, and a class designed by a singleton pattern can only instantiate 1 objects.

Class Singleton (object):
  __instance=none
  def__init__ (self): 
    pass
  def__new__ (CLS,*ARGS,**KWD):
    if Singleton.__instance is None: 
      singleton.__instance=object.__new__ (CLS,*ARGS,**KWD) return
    singleton.__instance

Single case mode is a common software design pattern. In its core structure, it contains only a special class called a single instance class. A single example mode can guarantee that a class in a system has only one instance and that the instance is easy to access outside, thus it is convenient to control the number of instances and save the system resources. Single mode is the best solution if you want to have only one object for a class in the system.

It is clear that there are three main points of the singleton pattern, one is that a class can have only one instance, and the other is that it must create the instance itself; third, it must provide this instance to the whole system.

From the point of view of concrete realization, it is the following three points: one is the class of the single example mode provides only the private constructor, and the second is that the class definition contains a static private object of the class, and third is that the class provides a static, shared function that creates or acquires its own static private object.

In the following object graph, there is a single instance object, and customer A, customer B, and customer C are three client objects of a single instance object. As you can see, all client objects share a single instance object. And from the single example object to its own connection line, it can be seen that the single Case object holds its own reference.

Some resource managers are often designed as a single case pattern.

In a computer system, resources that need to be managed include software external resources, such as each computer can have several printers, but only one printer Spooler to prevent two print jobs from being exported to the printer at the same time. Each computer can have several fax cards, but there should only be one software responsible for managing the fax card to avoid situations where two fax jobs are uploaded to the fax card at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to avoid a single communication port being invoked simultaneously by two requests.

The resources that need to be managed include the internal resources of the software, for example, most of the software has one (or even multiple) attributes (properties) file storage System configuration. Such a system should be managed by an object to manage a property file.

The internal resources that need to be managed include, for example, parts that are responsible for recording the number of visitors to the site, recording parts of the software system's internal events, error messages, or checking the performance of the system. These components must be centrally managed and not run out of bulls.

These resource manager artifacts must have only one instance, which is the first; they must initialize themselves, this is the second; Allow the entire system to access itself this is its third. Therefore, they all satisfy the condition of single case mode, and are the application of single case mode.

Advantages:

First, instance control

The singleton pattern prevents other objects from instantiating a copy of their own singleton objects, ensuring that all objects have access to a unique instance.

Second, flexibility

Because classes control the instantiation process, classes can flexibly change the instantiation process.

Disadvantages:

First, the cost

Although the number is small, some overhead will still be required if you want to check for instances of the class each time the object requests a reference. You can resolve this problem by using static initialization.

Ii. possible confusion of development

When working with Singleton objects, especially those defined in class libraries, developers must remember that they cannot instantiate objects using the New keyword. Because the library source code may not be accessible, application developers may accidentally find themselves unable to instantiate the class directly.

Iii. Object Lifetime

The problem of deleting a single object cannot be resolved. In a language that provides memory management, such as a language based on the. NET framework, only a singleton class can cause an instance to be unassigned because it contains a private reference to the instance. In some languages, such as C + +, other classes can delete an object instance, but this can result in a floating reference in a singleton class.

I hope this article will help you with your Python programming.

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.