# SingletonDemo.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2011-12-12 File created. # 2022-12-27 Converted to Python 3. # This program demonstrates how to implement # a design pattern called the singleton pattern. # In software engineering, the singleton pattern is # a design pattern used to implement the mathematical # concept of a singleton, by restricting the # instantiation of a class to one object. # This is useful when exactly one object is needed # to coordinate actions across the system. # The concept is sometimes generalized to systems # that operate more efficiently when only one object # exists, or that restrict the instantiation to a certain # number of objects. class SystemData : instance_to_return = None def __init__( self ) : self.some_data_member = 222 self.another_member = "Test Data" @staticmethod def get_instance() : if SystemData.instance_to_return == None : SystemData.instance_to_return = SystemData() return SystemData.instance_to_return # Testing system_data = SystemData.get_instance() print( system_data.some_data_member ) print( system_data.another_member )