Article View: borland.public.jbuilder.java.language
Article #7703Re: Singleton Pattern: Use All Static Methods or Private Constructor?
From: Michael Rachow
Date: Mon, 28 May 2001 21:31
Date: Mon, 28 May 2001 21:31
49 lines
2445 bytes
2445 bytes
Hi Jack <g> (just realizing what this could mean) I think that all techniques dealing with static methods do not create an instance of a class but do everything on the loaded class (only). If you try a (Borland) DataModule you will see a technique to create just one instance of a class. You would deal with such a class like you would do usually, e.g. Single one = new Single(); They designed the constructor in a way that the first new would create that single instance and all subsequent news will return the reference of that first instance. Look how they had done it! Greetings Michael Jack Frosch <jackfrosch[NO SPAM]@mail.com> schrieb in im Newsbeitrag: 3b128359$1_1@dnews... > 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? > > Thanks. > > Jack > >
Technical Details
Message-ID:
<3b12c34c$1_2@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!128.230.129.106!news.maxwell.syr.edu!newsfeed1.uni2.dk!newssuck.uni2.dk!dnewsReferences:
<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 14:43:40 PST From: "Michael Rachow" <michael@famrachow.de> Newsgroups: borland.public.jbuilder.java.language References: <3b128359$1_1@dnews> Subject: Re: Singleton Pattern: Use All Static Methods or Private Constructor? Date: Mon, 28 May 2001 23:31:57 +0200 Lines: 51 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 NNTP-Posting-Host: 217.81.81.19 Message-ID: <3b12c34c$1_2@dnews> X-Trace: dnews 991085388 217.81.81.19 (28 May 2001 14:29:48 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!128.230.129.106!news.maxwell.syr.edu!newsfeed1.uni2.dk!newssuck.uni2.dk!dnews