English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] How to implement "Singleton" design pattern?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-08-03 (17:52)
From: tim@f...
Subject: Re: [Caml-list] How to implement "Singleton" design pattern?
>(a) invisible class definition in a module

Yes.  Singleton.ml has:

   class type singleton = object
     method whatever: int

   class singleton_impl: singleton = object
     method whatever: int = 7

   let theSingleton: singleton option ref = ref None

   let getInstance (): singleton =
     match !theSingleton with
         None ->
           let result = new singleton_impl
             theSingleton := Some result;
       | Some result -> result
Singleton.mli has:

   class type singleton = object
     method whatever: int
   val getInstance: unit -> singleton

>(b) the class definition which can't be called with "new" (private class ?)

I don't know how to do that except by hiding it with a signature as above.

>(c) local class definition (in another class definition ?)

I don't think you can nest class definitions.
Tim Freeman       
GPG public key fingerprint ECDF 46F8 3B80 BB9E 575D  7180 76DF FE00 34B1 5C78 
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners