Join Jungwoo Ryoo for an in-depth discussion in this video Singleton example, part of Python: Design Patterns.
- To implement the singleton pattern, we use this Borg class which makes the class attributes global. Let's see how we can make our class attributes global. First of all, we'll be declaring an attribute which is a dictionary. Type _shared _state and we'll make it a dictionary. Then these user-defined dictionary data structure will be used to initialize the attribute dictionary of the class.
Simply type self.__ d-i-c-t__ and then assignment, self._ shared_state. Again, this makes this _shared_state our attribute dictionary. Once we define our Borg class, then we can inherit from it by putting Borg right here.
So then the Singleton class, in this case, is inheriting from the Borg class as a parent. Now, what we have done is that by inheriting from the Borg class, this class now shares all its attributes among all its various instances. So whenever we create an instance of the Singleton class, it will be sharing the same set of attributes together in the dictionary. This essentially makes the singleton objects an object-oriented form of global variable.
Now, the goal of our singleton object is to make it a dictionary of acronyms which is why we use this statement. So type self ._ shared_state.update and (kwargs). What we're doing here is to use this argument of the init method and then accepting a dictionary item through the argument and then update our attribute dictionary with the newly provided dictionary item.
Essentially, we're maintaining our dictionary of acronyms every time this new object gets created and we have a new acronym. Then the last method is simply returning the attribute dictionary for printing when the print function is used. Simply type return str(self._ shared_state).
The implementation of singleton isn't too bad. Now, let's test our Singleton class. Let's first create a singleton object and add our first acronym. Type x is our variable and we'll create a Singleton which has its first acronym, (HTTP = "Hyper Text Transfer protocol"). Then we'll try to print the object.
So print(x). We'll try to run the program and see what happens. Tools, Build. There is another typo here which is supposed to be lowercase update. Then Tools, Build one more time, and there is a white space so I need to be removing the white space. Let's try again. Tools, Build. And it works. Now, what you see is when we try to print the singleton object, it shows the content of its attribute dictionary which is the key and the value mapping of our acronym and then spelled out version of the acronym.
Then let's see what happens when we try to create another singleton object and try to add another acronym. Now, we use variable y and Singleton. The acronym is (SNMP = "Simple Network Management Protocol"). We're essentially using the same attribute dictionary and we're simply adding a new item to the attribute dictionary.
What's going to happen is it will retain the previous acronym and then add this new acronym to the attribute dictionary. Let's try to print it and see the result. Let's run the script. As you can see, it kept HTTP and its spelled out version, Hyper Text Transfer Protocol, and it added new acronym mapping SNMP, Simple Network Management Protocol.
The way singleton is implemented is a lot different from the way singleton is implemented in other languages and Python's way of implementing singleton, it's much, much simpler.
- Understanding design patterns
- Best design practices: consistency, completeness, and correctness
- Working with creational patterns
- Working with structural patterns
- Working with behavioral patterns
Skill Level Intermediate
Q: In the strategy pattern example, why does the code keep executing the default function rather than the alternate?
A: The programming demonstration skips the step of defining what the strategy pattern should be when an alternate function name is provided
as an argument as shown below.