Skip to content
This repository has been archived by the owner on Oct 16, 2019. It is now read-only.

tomerghelber/singleton-factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status

singleton_factory

Python implements for singleton factory pattern.

As it's sound, an object that can create a lot of instance but each instance is a singleton (can be created only once). Requiring the same object with the same parameters on __init__ will return the existing object.

Usage

SingletonFactory is a type so we need to put it as metaclass:

from singleton_factory import SingletonFactory


class SingletonFactoryA(object, metaclass=SingletonFactory):
    def __init__(self, name):
        self.name = name

    def __hash__(self):
        return hash(self.name)

Additional, we want to override the function of hash so we will control when to instances are equals. We are doing that in case that an object can get some arguments in init and create the same objects (or wanted object). That means the object will be created twice but only the oldest one will be returned. The use is regularly. See the tests for more data.

About

implements of singleton factory

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages