[Xotcl] Help with singleton design pattern

Uwe Zdun uwe.zdun at wu-wien.ac.at
Wed Apr 23 14:15:50 CEST 2003


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 at alice.wu-wien.ac.at
> > http://alice.wu-wien.ac.at/mailman/listinfo/xotcl

-- 
Uwe Zdun
Department of Information Systems, Vienna University of Economics
Phone: +43 1 313 36 4796, Fax: +43 1 313 36 746
zdun@{xotcl,computer,acm}.org, uwe.zdun at wu-wien.ac.at





More information about the Xotcl mailing list