From the course: Python: Design Patterns
Unlock the full course today
Join today to access over 24,200 courses taught by industry experts.
Singleton - Python Tutorial
From the course: Python: Design Patterns
Singleton
- [Narrator] According to the Gang of Four definition. Singleton is the pattern you need, When you want to allow only one object to be created from a class. It's an object oriented way of providing global variables. A similar concept called Borg exists in the Python community. Unlike Singleton. Borg allows multiple object instances, but they all share the same state. Which means the same attribute values in the objects. Borg is a short for cyborg prominently featured in the Star Trek series. They share collective dots like objects in the Python Borg pattern. Why would you need a pattern like Singleton or Borg? Let's say that there is a need for keeping a cache of information to be shared by various elements of yourself in the system. By keeping this information in a single object like Singleton or sharing it constantly in Borg objects, There is no need to retrieve the information from its original sources each time. All…