an add-on to this variant: Gustaf mentioned that you need an overloaded create method here, if you want to enforce the Singleton property:
A instproc create args {error "cannot create Singleton instance"}
--Uwe
On Tuesday 22 April 2003 22:17, Uwe Zdun wrote:
Hi,
I just had the idea for yet another Singleton variant that was not exactly planned for in the XOTcl design, but seems to work fine: an object that has itself as a class (and Class as a superclass so that it stays a class):
% Class A -superclass Class -class A
that means it can define instprocs on it and dispatch them directly:
% A instproc aProc args {puts ****} % A aProc
procs do also work:
% A proc bProc args {puts ++++} % A bProc ++++
and you can derive a subclass from the Singleton:
% Class B -superclass A
::B
% B b
::b
% b aProc
and it conforms to the "Singleton definiton" (a class that has just one instance).
--Uwe
On Tuesday 22 April 2003 21:21, Artur Trzewik wrote:
Hallo!
As singleton objects I often use Objects directly derived from Object
Object MySingleton MySingleton proc doJob {} { puts "This is my job" }
MySingleton doJob
This is not singleton pattern but is same cases you do not need some Java, C++ solution. One disadvantage is that you can not use method inheritance.
By the way. The presented solution from G. Neumann. That is to overwrite "new" method that returns singleton object is the standard way to implements singleton in Smalltalk (the singleton instance is saved in class variable Default). That is XOTcl! "new" is just method not special operator and can be overwritten.
Artur Trzewik
Xotcl mailing list - Xotcl@alice.wu-wien.ac.at http://alice.wu-wien.ac.at/mailman/listinfo/xotcl