Join Jungwoo Ryoo for an in-depth discussion in this video Singleton, part of Python: Design Patterns.
- An object-oriented way of providing global variables is Singleton. Singleton is the pattern you need when you'd like to allow only one object to be instantiated from a class. Like I said, creating a global variable in an object-oriented way is Singleton. Why would you need such a pattern? Let's say that there is a need for keeping a cache of information to be shared by various elements of your software system. By keeping this information in the single object, there's no need to retrieve the information from its original source every time.
So in this case, Singleton acts as a cache of the information. Modules in Python act as Singletons. There are also a number of ways of implementing Singleton, but we'll be using the Borg design pattern to implement our Singleton.
- 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.
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.