[plt-scheme] Singleton pattern using class.ss

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Dec 12 11:15:38 EST 2007

Please feel free to use the example in any which way you want. --  
Matthias



On Dec 12, 2007, at 9:33 AM, Stephen De Gabrielle wrote:

> Thank  you!
>
> Yes it does help.
>
> I'm trying to work out the class.ss equivalent of java class  
> methods and fields like this;
>
> public class ActivityManager extends Thread {
> [...]
>    private synchronized static ActivityManager getInstance() {
>        if ( s_instance == null || !s_instance.isAlive() ) {
>            s_instance = new ActivityManager();
>        }
>        return s_instance;
>    }
> [code from prefuse.org; I'm porting bits to scheme]
>
> Do you mind if I put your example in the scheme cookbook?
> (in or near http://schemecookbook.org/Cookbook/ 
> IntroductionToMzlibClasses )
>
> The 'Factory Pattern' is next for me. Any suggestions gratefully  
> accepted.
>
> Cheers,
>
>
> Stephen
>
>
>
>
>
> On Dec 11, 2007 2:58 AM, Matthias Felleisen <matthias at ccs.neu.edu>  
> wrote:
> > On Dec 10, 2007, at 8:48 PM, Stephen De Gabrielle wrote:
> >
> > > Hi, I'm trying to replicate the 'singleton' pattern using  
> class.ss,
> > > but I'm having a little trouble. Can anyone point of to an  
> example?
> >
> >
> > One of the things you can do is instantiate a class once and export
> > only that instance:
> >
> > (module singleton mzscheme
> >
> >    (require (lib "class.ss"))
> >
> >    (define one%
> >      (class object%
> >        (super-new)
> >        (define/public (hello) one))
> >
> >    (define one (new one%))
> >
> >    (provide one))
> >
> > Inside the class you never use new; instead you refer to this
> > instance. Does this help?
> >
> > -- Matthias
> >
> >
> >
>
>
>
> -- 
> Cheers,
>
> Stephen
>
>
>
> --
> Stephen De Gabrielle
> s.degabrielle at ucl.ac.uk
> Telephone +44 (0)20 7679 5242 (x45242)
> Mobile                  079 851 890 45
> http://www.uclic.ucl.ac.uk/annb/MaSI.html
> University College London Interaction Centre
> Remax House - 31/32 Alfred Place
> London - WC1E 7DP
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.