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.…
- 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.
Programming Foundations: Design Patternswith Elisabeth Robson2h 19m Intermediate
1. Understanding Design Patterns
2. Creational Patterns
3. Structural Patterns
4. Behavioral Patterns
5. Design Best Practices
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.