[Zope] Re: Using Singletons

Tres Seaver tseaver at palladion.com
Wed Sep 28 14:02:14 EDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Doyon, Jean-Francois wrote:
> Hello,
> 
> Is there any support within the framework for singletons? What I call
> singletons are objects that are instanciated and persisted only once, or at
> least should be.
> 
> I find myself wanting to use these in several instances, notably when I
> create a class/object that acts as a gateway to an external system of some
> kind (like a web service).
> 
> I create an object that provides a pythonic/zope-ish API to the rest of my
> objects/product code.  I often like to use a persistent object (vs. a
> regular python module/package) in order to take advantage of things like
> caching, or giving options for configuration through the ZMI.
> 
> Given this use case, it would be nice to have Zope provide a framework to
> support such things.  It could enforce the uniqueness of the object in the
> entire system (absolutely no way to add more than one), and maybe provide a
> global mechanism to "find" the object not needing to know where in the tree
> it is, what it is called, or using the catalog (rather expensive?).  Maybe
> something like a singleton flag and registry of somekind?
> 
> I can kind of achieve this partly by doing things like "visible=0" is my CMF
> registration for example, forcing a specific id, enforcing rules in the
> manage_add type methods, and so on, but it all seems so kludgy.  Not to
> mention I always have to know the name of the object, and possibly even the
> path, to get to it ... Redundant when there's always only one!
> 
> I'm not looking for solutions, but ideas and thoughts ... Although if
> someone knows of ways to do this nicely, maybe with a third-party product
> for example, I'd love to hear about it!

CMF uses a base class called UniqueObject for its tools, which are
normally "singletons":

  http://svn.zope.org/CMF/trunk/CMFCore/utils.py?rev=38507&view=auto

In Zope3, one accomplishes your use case by registering a utility for
the interface your singleton provides, without supplying a name.


Tres.
- --
===================================================================
Tres Seaver          +1 202-558-7113          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDOtqm+gerLs4ltQ4RAmdqAJ9qE9SCVEtrlM+GnemGtuOhTf8sXwCeKbDS
Se0IViIBS9fP8oM9+RPrgxw=
=7DOZ
-----END PGP SIGNATURE-----



More information about the Zope mailing list