Yo.
So, in the last post, we discussed the Builder pattern. Today, we're gonna discuss a similar pattern. The Singleton, another one of the creational design patterns. Let's get into it.
What is it, and why use this pattern?
The Singleton pattern is a creational design pattern. That means it's used to create new objects.
This pattern is used to ensure that your class only has one instance. And it provides a global access point to that instance. I'll explain what all of this means.
The best example I can give you is of a database connection. You only really want one connection in your program. You don't want multiple objects connecting and interacting with the same database.
In other words, you want a Database
class with only a single instance. That's where the Singleton pattern can help you.
In fact, this pattern is very useful whenever you want to control access to any shared resource, whether that's a database or a file or some API, etc.
But without going too deep into specific examples, if you want to only have one instance of your object, you can use the Singleton pattern.
How to use it?
Let's get practical. I'll create a Python class to interact with a Redis cache, and I'll use the Singleton pattern on it.
This Cache
class will be used to cache data in a Redis database. We have some pretty simple and self-explanatory methods here. get()
and set()
will get and set values, while delete()
will delete a given value. And clear()
will delete the whole database.
You can see in the __init__()
method that we create a connection
object. We only want one connection to the database, and we want to keep all the required state in this class so it's all organized.
Any client code that wants to use this cache will use the cache
object we created at the end. They will not create a new instance.
Preventing multiple instances (Python)
But, then again, there's nothing that's actually stopping us from creating a new instance. We could just do that as many times as we like:
This would be terrible of course. Having so many open connections to a single database.
How can we prevent this?
In other languages, you would make the constructor method private. You can't actually do that in Python, since Python doesn't have the concept of public and private variables. So, let's see what we can do here.
I added a connection
variable to the class. I now set this connection
variable instead of setting an instance-based variable using self
.
Now, what happens is:
When you create your first instance, it will create a new DB connection and set it to the
connection
variable.If you try to create multiple instances after that, it will just ignore that call because our IF condition there would return
False
.In all cases, it will set
self.connection
to the connection object as well, so we have the same connection in every instance we create.
It's only in Python that we have to use this workaround. In other languages that implement public and private variables, we would simply make the constructor method private.
Caveats
One thing to keep in mind if you're designing a multi-threaded program: make sure your threads aren't creating multiple instances in any way. Threading can make programs much more complicated.
That's about it for this post. Personally speaking, the Singleton pattern is very easy to apply in most code. So go ahead and put this thing into action. Let me know how it goes for you. :)