课程: Python: Design Patterns (2021)
Singleton example
- [Instructor] To implement the singleton pattern, we use this Borg class that makes its attributes global. Let's see how we can make our class attributes global. First, we'll be declaring an attribute that is a dictionary. Type _shared_data, and then, we'll make it a dictionary. So equal sign, space, squiggly brackets. Then, we'll use this user-defined dictionary data structure to initialize the attribute of the class. Type def, two underscores, init, two underscores, self, column. Now type self.__dict__, space, assignment, space, self._shared_data. This arrangement makes the _shared_data our attribute dictionary. Once we define our Borg class, you can inherit from it by putting Borg right here. There's an argument for the Singleton class, constructor. By inheriting from the Borg class, the Singleton class can share the attributes among all its various instances. Whenever we create an instance of the Borg class, it will share the same set of attributes in the dictionary. This setup essentially makes the Borg object an object-oriented form of global variables. Our example, Borg object here is to maintain a dictionary of acronyms. Type D, E, F, space, __init, self, star, star, kwargs, column Borg.__init, self. Now type self._shared_data.update(kwargs). What we're doing here is to use the argument of the init method to accept a dictionary item, and then update our attribute dictionary with the newly provided dictionary item. Essentially, we're keeping our dictionary of acronyms every time a new object gets created. The last method is returning the attribute dictionary for printing, when the print function is used. Type def __str__(self): return str(self._shared_data). So this returns the attribute dictionary for printing. Let's test our Singleton class by first creating a singleton object and adding our first acronym. Type x, our variable, space, assignment, space, and we'll create a singleton object to contain its first acronym. Type Singleton, parentheses, HTTP, space, equals sign, space, Hyper Text Transfer Protocol. Note that HTTP is our key. The equal sign associates the key with a string value, Hyper Text Transfer Protocol. Next, we'll print the object. Type print(x). Print, parentheses, x. Let's run the program and see what happens. Go to Tools, click on Build. It works. The output shows the content of the Borg attribute dictionary, which is a key and value pair of our acronym and its spelled-out version. Let's see what happens when we try to create the second singleton object and add another acronym. We use variable Y, space, equal sign, space, Singleton, parentheses. The acronym is SNMP, space, equal sign, double quotes, Simple Network Management Protocol. We're using the same attribute dictionary and adding a new item to it. What's going to happen is that the attribute dictionary will retain the previous acronym, and then add this new acronym to itself. Let's print it and see the result. Type print, parentheses, y. Let's run the script. Go to Tools, Build. As you can see, the second Borg object kept HTTP and it spelled that version, Hyper Text Transfer Protocol, and it added the new acronym definition pair, SNMP, Simple Network Management Protocol. The way we implement Singleton here is different from its classic version. But Python's way still does the job, maybe even better.