Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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
   end

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

   let theSingleton: singleton option ref = ref None

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

   class type singleton = object
     method whatever: int
   end
      
   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       
tim@fungible.com
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