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 (05:29)
From: SooHyoung Oh <shoh@d...>
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

    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