Creational

Singleton

Description

Ensures a class has only one instance and provides a global point of access to it. Often used for shared resources.

Where to use

  • Logging drivers.
  • \n
  • Database connections.
  • \n
  • Configuration settings.

Real World Example

A 'Logger' class where every part of the app writes to the same log file via the same instance.

Code Example

class Singleton:\n    def __init__(self):\n        pass\n\n    def execute(self):\n        print('Executing Singleton')