- Learning Python Design Patterns(Second Edition)
- Chetan Giridhar
- 428字
- 2021-07-16 09:46:15
Understanding the Singleton design pattern
Singleton provides you with a mechanism to have one, and only one, object of a given type and provides a global point of access. Hence, Singletons are typically used in cases such as logging or database operations, printer spoolers, and many others, where there is a need to have only one instance that is available across the application to avoid conflicting requests on the same resource. For example, we may want to use one database object to perform operations on the DB to maintain data consistency or one object of the logging class across multiple services to dump log messages in a particular log file sequentially.
In brief, the intentions of the Singleton design pattern are as follows:
- Ensuring that one and only one object of the class gets created
- Providing an access point for an object that is global to the program
- Controlling concurrent access to resources that are shared
The following is the UML diagram for Singleton:

A simple way of implementing Singleton is by making the constructor private and creating a static method that does the object initialization. This way, one object gets created on the first call and the class returns the same object thereafter.
In Python, we will implement it in a different way as there's no option to create private constructors. Let's take a look at how Singletons are implemented in the Python language.
Implementing a classical Singleton in Python
Here is a sample code of the Singleton pattern in Python v3.5. In this example, we will do two major things:
- We will allow the creation of only one instance of the
Singleton
class. - If an instance exists, we will serve the same object again.
The following code shows this:
class Singleton(object): def __new__(cls): if not hasattr(cls, 'instance'): cls.instance = super(Singleton, cls).__new__(cls) return cls.instance s = Singleton() print("Object created", s) s1 = Singleton() print("Object created", s1)
The output of the preceding snippet is given here:

In the preceding code snippet, we override the __new__
method (Python's special method to instantiate objects) to control the object creation. The s object gets created with the __new__
method, but before this, it checks whether the object already exists. The hasattr
method (Python's special method to know if an object has a certain property) is used to see if the cls
object has the instance property, which checks whether the class already has an object. Till the time the s1
object is requested, hasattr()
detects that an object already exists and hence s1
allocates the existing object instance (located at 0x102078ba8
).
- Java系統(tǒng)分析與架構(gòu)設(shè)計(jì)
- Beginning Java Data Structures and Algorithms
- Vue.js前端開發(fā)基礎(chǔ)與項(xiàng)目實(shí)戰(zhàn)
- 高級C/C++編譯技術(shù)(典藏版)
- Mastering Rust
- MINECRAFT編程:使用Python語言玩轉(zhuǎn)我的世界
- Android編程權(quán)威指南(第4版)
- 深入淺出 HTTPS:從原理到實(shí)戰(zhàn)
- Mastering ASP.NET Web API
- Android項(xiàng)目實(shí)戰(zhàn):博學(xué)谷
- 零基礎(chǔ)學(xué)Java(升級版)
- Python High Performance(Second Edition)
- PHP程序開發(fā)參考手冊
- Java實(shí)例精通
- Responsive Web Design with HTML5 and CSS3 Essentials