From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id HAA12923; Sat, 3 Aug 2002 07:29:00 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id HAA13279 for ; Sat, 3 Aug 2002 07:28:59 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from www.duonix.com ([210.113.163.221]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g735Suf01226 for ; Sat, 3 Aug 2002 07:28:57 +0200 (MET DST) Received: from hama ([192.168.0.254]) by www.duonix.com (8.12.5/8.11.6) with SMTP id g735WJQG046713 for ; Sat, 3 Aug 2002 14:32:20 +0900 (KST) (envelope-from shoh@duonix.com) X-Authentication-Warning: www.duonix.com: Host [192.168.0.254] claimed to be hama Message-ID: <002d01c23aae$b44a6490$fe00a8c0@hama> From: "SooHyoung Oh" To: "Caml-List" Subject: [Caml-list] How to implement "Singleton" design pattern? Date: Sat, 3 Aug 2002 14:29:13 +0900 MIME-Version: 1.0 Content-Type: text/plain; charset="ks_c_5601-1987" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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