caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] How to implement  "Singleton" design pattern?
@ 2002-08-03  5:29 SooHyoung Oh
  2002-08-03 17:42 ` Tim Freeman
  2002-08-05  9:28 ` Hendrik Tews
  0 siblings, 2 replies; 4+ messages in thread
From: SooHyoung Oh @ 2002-08-03  5:29 UTC (permalink / raw)
  To: Caml-List


It seems that applying design pattern to Ocaml isn't difficult.
While I've been testing some of them,
I found a strange design pattern called "Singleton".

In Java, "Singleton" design pattern looks like this:

    public class Singleton {
        private static Singleton singleton = new Singleton ();
        private Singleton () {
            ...
        }
        public static Singleton getInstance () {
            return singleton;
        }
    }

and in main, it doesn't use "new" but use "getInstance()" for get THE shared
instance.

    {
    ...
    Singleton obj1 = Singleton.getInstance ();
    Singleton obj2 = Singleton.getInstance ();
    ...
    }

I think that the key point of Singleton class is
(1) can't use "new Singleton ()"
because the constructor is private.
(2) "static Singleton singleton" which keeps the only one Singleton object
and it is shared between all instances.

The (2) seems to be possible using "Class variable".
And then how can we solve the (1)?

There are some possible solutions:
(a) invisible class definition in a module
(b) the class definition which can't be called with "new" (private class ?)
(c) local class definition (in another class definition ?)

Which one/ones is/are feasible and/or good in Ocaml?
And are there any other solutions?

---
SooHyoung Oh
tel: 02)583-8709, 042)861-8649
cell. phone: 011-453-4303
web: http://www.duonix.com


-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] How to implement  "Singleton" design pattern?
  2002-08-03  5:29 [Caml-list] How to implement "Singleton" design pattern? SooHyoung Oh
@ 2002-08-03 17:42 ` Tim Freeman
  2002-08-05  9:28 ` Hendrik Tews
  1 sibling, 0 replies; 4+ messages in thread
From: Tim Freeman @ 2002-08-03 17:42 UTC (permalink / raw)
  To: shoh; +Cc: caml-list

>(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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] How to implement  "Singleton" design pattern?
  2002-08-03  5:29 [Caml-list] How to implement "Singleton" design pattern? SooHyoung Oh
  2002-08-03 17:42 ` Tim Freeman
@ 2002-08-05  9:28 ` Hendrik Tews
  1 sibling, 0 replies; 4+ messages in thread
From: Hendrik Tews @ 2002-08-05  9:28 UTC (permalink / raw)
  To: Caml-List

SooHyoung Oh writes:
   Date: Sat, 3 Aug 2002 14:29:13 +0900
   Subject: [Caml-list] How to implement  "Singleton" design pattern?
   
   And then how can we solve the (1)?
   
   There are some possible solutions:
   (a) invisible class definition in a module
   (b) the class definition which can't be called with "new" (private class ?)
   (c) local class definition (in another class definition ?)
   
   Which one/ones is/are feasible and/or good in Ocaml?
   And are there any other solutions?
   
If it is enough to detect the error at runtime:
You can use an initializer that raises an exception if a second
object is created. 

Bye,

Hendrik
-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [Caml-list] How to implement  "Singleton" design pattern?
@ 2002-08-05 17:48 Gurr, David (MED, self)
  0 siblings, 0 replies; 4+ messages in thread
From: Gurr, David (MED, self) @ 2002-08-05 17:48 UTC (permalink / raw)
  To: SooHyoung Oh, Caml-List

SML (esp. Mosml) & Ocaml have modules. -D

> -----Original Message-----
> From: SooHyoung Oh [mailto:shoh@duonix.com]
> Sent: Friday, August 02, 2002 10:29 PM
> To: Caml-List
> Subject: [Caml-list] How to implement "Singleton" design pattern?
> 
> 
> 
> It seems that applying design pattern to Ocaml isn't difficult.
> While I've been testing some of them,
> I found a strange design pattern called "Singleton".
> 
> In Java, "Singleton" design pattern looks like this:
> 
>     public class Singleton {
>         private static Singleton singleton = new Singleton ();
>         private Singleton () {
>             ...
>         }
>         public static Singleton getInstance () {
>             return singleton;
>         }
>     }
> 
> and in main, it doesn't use "new" but use "getInstance()" for 
> get THE shared
> instance.
> 
>     {
>     ...
>     Singleton obj1 = Singleton.getInstance ();
>     Singleton obj2 = Singleton.getInstance ();
>     ...
>     }
> 
> I think that the key point of Singleton class is
> (1) can't use "new Singleton ()"
> because the constructor is private.
> (2) "static Singleton singleton" which keeps the only one 
> Singleton object
> and it is shared between all instances.
> 
> The (2) seems to be possible using "Class variable".
> And then how can we solve the (1)?
> 
> There are some possible solutions:
> (a) invisible class definition in a module
> (b) the class definition which can't be called with "new" 
> (private class ?)
> (c) local class definition (in another class definition ?)
> 
> Which one/ones is/are feasible and/or good in Ocaml?
> And are there any other solutions?
> 
> ---
> SooHyoung Oh
> tel: 02)583-8709, 042)861-8649
> cell. phone: 011-453-4303
> web: http://www.duonix.com
> 
> 
> -------------------
> 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
-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-08-05 17:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-03  5:29 [Caml-list] How to implement "Singleton" design pattern? SooHyoung Oh
2002-08-03 17:42 ` Tim Freeman
2002-08-05  9:28 ` Hendrik Tews
2002-08-05 17:48 Gurr, David (MED, self)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).