From 995ebfbfce2b5e96cbc30f7583192dd11fbc68fb Mon Sep 17 00:00:00 2001 From: Farheen Shabbir Shaikh Date: Sun, 8 Jun 2025 17:47:15 -0700 Subject: [PATCH] Add Singleton design pattern example --- patterns/singleton.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 patterns/singleton.py diff --git a/patterns/singleton.py b/patterns/singleton.py new file mode 100644 index 00000000..0cbdf2f5 --- /dev/null +++ b/patterns/singleton.py @@ -0,0 +1,17 @@ +""" +Singleton Pattern Example +Ensures a class has only one instance and provides a global point of access to it. +""" + +class Singleton: + _instance = None + + def __new__(cls): + if not cls._instance: + cls._instance = super(Singleton, cls).__new__(cls) + return cls._instance + +if __name__ == "__main__": + obj1 = Singleton() + obj2 = Singleton() + print("Are obj1 and obj2 the same?", obj1 is obj2) \ No newline at end of file