lundi 5 août 2019

Python singleton design pattern - static method vs class method appraoch

Foll. is a sample singleton design pattern implementation using static method. My question is - what difference would it have made if we use class method instead of static method?

class Singleton:
   __instance = None
   @staticmethod 
   def getInstance():
      """ Static access method. """
      if Singleton.__instance == None:
         Singleton()
      return Singleton.__instance
   def __init__(self):
      """ Virtually private constructor. """
      if Singleton.__instance != None:
         raise Exception("This class is a singleton!")
      else:
         Singleton.__instance = self
s = Singleton()
print s

s = Singleton.getInstance()
print s

s = Singleton.getInstance()
print s

Aucun commentaire:

Enregistrer un commentaire