Article View: borland.public.jbuilder.java.language
Article #7701Re: Singleton Pattern: Use All Static Methods or Private Constructor?
From: Joi Ellis [TeamB]
Date: Mon, 28 May 2001 17:28
Date: Mon, 28 May 2001 17:28
30 lines
2104 bytes
2104 bytes
Jack Frosch <jackfrosch[NO SPAM]@mail.com> wrote: > IN their book "Design Patterns" the GoF defines the Singleton pattern's > intent thus: "To ensure a class has only one instance, and provide a global > point of access to it." > For Java implementations, I've seen two implementation variations to achieve > this: > 1) Some people code all static methods and data in a class, thus ensuring > only one "instance" of the class exists in the VM. > 2) Others use an implementation where the class is defines as normal, but > with a private constructor. A single public, static method is provided to > the single instance of the class that's stored as a private instance > variable. > Approach #2 clearly meets the pattern description of the GoF, but I'm > wondering if there are any uniquely-Java reasons why approach #1 might be > preferable? I've used something like #1 to control JNI libraries. I use #2 to control pseudo-singletons that handle tcp listen sockets. (There can be only one listener on a port, but I can have multiple listeners on separate ports.) -- Joi Ellis (TeamB) No direct email, please. For best results, post on news server newsgroups.borland.com! Newsgroup Info: http://www.borland.com/newsgroups/guide.html
Technical Details
Message-ID:
<3b128ac9_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews!not-for-mailReferences:
<3b128359$1_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,604521ee4b628609
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-05-28 10:52:08 PST
Message-ID: <3b128ac9_1@dnews>
From: "Joi Ellis [TeamB]" <joi@aravox.com>
Subject: Re: Singleton Pattern: Use All Static Methods or Private Constructor?
Newsgroups: borland.public.jbuilder.java.language
References: <3b128359$1_1@dnews>
User-Agent: tin/1.4.2-20000205 ("Possession") (UNIX) (Linux/2.2.16-3 (i686))
NNTP-Posting-Host: 209.46.41.66
Date: 28 May 2001 10:28:41 -0700
X-Trace: dnews 991070921 209.46.41.66 (28 May 2001 10:28:41 -0700)
Lines: 30
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews!not-for-mail