Good time!

The other day I ran into the task: to expand Singleton so that at the same time it was possible to have several different Singlotons based on the same class ... the idea itself was not hard to implement, but it is difficult to call singleton alone because of the class instance in memory is not one, but several ...

And actually the subject itself: to what pattern does this miracle belong? discussion in the working environment and multi-threaded Google (ing) did not lead to anything at least a bit similar description ...

#!/usr/bin/python # -*- coding:u8 -*- __author__ = '_killed_' class Singleton(object): __instance = {} #__tags={} def __new__(cls,*a,**k): __singleton_name__ = k.get('__singleton__',None) if not cls.__instance: cls.__instance[__singleton_name__] = super(Singleton, cls).__new__(cls,*a,**k) return cls.__instance[__singleton_name__] else: if __singleton_name__ not in cls.__instance: cls.__instance[__singleton_name__] = super(Singleton, cls).__new__(cls,*a,**k) return cls.__instance[__singleton_name__] else:return cls.__instance[__singleton_name__] def __init__(self,*argc,**kwargs): self.__dict__.update(kwargs) def __str__(self): return self.__singleton__ single = Singleton() c1 = [Singleton(__singleton__='c1') for i in xrange(5)] c2 = [Singleton(__singleton__='c2') for i in xrange(5)] c3 = [Singleton(__singleton__='c3') for i in xrange(5)] for i in [c1,c2,c3]: print map(str,i) print map(id,i) print map(hash,i) print "*"*30 print single._Singleton__instance 

We look at the code here: http://paste.org/52784 . PS: I would be grateful if the experts show interest.

  • one
    I am tormented by one question, am I the only such fool, that the Python code is poorly read for me? PS: yes, on the python did not write - Alex Kapustin

2 answers 2

Multiton pattern

In software development, multiton is a template similar to singleton, which allows the creation of only one instance of a class. The multiton model extends the singleton concept for managing a map of named instances as key-value pairs.

  • Thank you, for some reason this did not come across to us ... - Alexander Molofeev

I do not like the tight-fitting classification and the desire to cram everything into some "fashionable" framework (this is me about patterns ).

In your case, I would talk about the object pool.