[sugar] Singleton

Marco Pesenti Gritti mpg
Sat Aug 26 07:59:06 EDT 2006


we should probably settle on a consistent implementation for singletons.

This is what we have in the ps:

_ps = None
def get_instance():
        global _ps
        if not _ps:
                _ps = PresenceService()
        return _ps

and PresenceService.get_instance() to get it.

In the icon cache:

class IconCache(gobject.GObject):
    __metaclass__ = GObjectSingletonMeta

using this from sugar.util

class GObjectSingletonMeta(gobject.GObjectMeta):
    """GObject Singleton Metaclass"""

    def __init__(klass, name, bases, dict):
        gobject.GObjectMeta.__init__(klass, name, bases, dict)
        klass.__instance = None

    def __call__(klass, *args, **kwargs):
        if klass.__instance is None:
            klass.__instance = gobject.GObjectMeta.__call__(klass, 
*args, **kwargs)
        return klass.__instance

and you can use the normal constructor.



More information about the Sugar-devel mailing list