Singleton provides a base class that easily enables singleton-like behavior in it's subclasses. In it's simplest form, the constructor for subclasses of Singleton will create a new instance of the object if it does not exist - otherwise it will return the existing instance.
Subclasses of Singleton can override several methods to provide custom behavior for the subclass. *createNew is called when a new object needs to be constructed (this provides a parallel to the *new method). -init is called once the object is created. Both are guaranteed to be called ONLY when a new object is created. -set is called on the object EVERY TIME the Singleton constructor is invoked.
The simplest example of using the Singleton class is of the following form:
Usage:
The Singleton constructor accepts a key that is used to look up the backing object, so multiple singleton objects can be stored for a single class. In this way, it behaves similarly to common "global registry" objects like Ndef, Pdef, etc.
Remaining arguments to the Singleton constructor are passed to the object's set
method. These can be used to set the internal state of the object. For example, if we had:
Then we could:
The set
method can be called directly as well:
Singleton supports the know
flag like Environments, so dot notation can be used to create and access Singleton's as well:
When using this pattern, the right-hand-side of the equals is passed in as the first argument - this is problematic in cases where set
can take more than one argument. One way to keep the elegance of dot notation while allowing more complex arguments is to detect the type of arguments in your set
method. For example:
For Singleton subclasses to work properly, Singleton:*new
must be called. This should generally not be overridden by subclasses.
name |
A name identifying the Singleton object. The internal storage is identity-based, so Symbol's should be used instead of String's. |
...settings |
Arguments to be forwarded to the |
An instance of the Singleton object.
This returns a new instance of your class. Override this in a subclass to provide custom behavior when constructing a new instance of your class - this is the equivalent of the normal *new
method. This should not be called directly.
Returns an IdentityDictionary of all Singletons registered for this class.
If set to true, the Singleton interprets method calls as calls to the Singleton constructor. With know = true
, the following are equivalent:
This is called each time the constructor for Singleton is invoked, regardless of whether a new object is created or not. Arguments to -new are forwarded to set
. Override this to allow Singleton calls to set internal properties of your object.
This is called on every new Singleton object. Override this to add behavior that occurs when a new object is created.
The name of the Singleton object.
Remove / clear this specific Singleton object. The next time the Singleton constructor is called, it will create a new object.