🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #18415

Re: Global Singleton Classes?

#18415
From: Gerry Mantha
Date: Sat, 27 Jun 1998 00:00
58 lines
2436 bytes
Java handles the singleton model very well... example:

class MySingleton(){
    MySingleton  mySingleton;   // defaults to null
    ... your properties....

    private MySingleton(){         // note the private constructor
    }

   public static MySingleton getMySingleton(){
       if(mySingleton == null) mySingleton = new MySingleton();
       return mySingleton;
   }

   public PropertyWhatever1 getPropertyWhatever1(){
       return propertyWhatever1;
   }
   public  setPropertyWhatever1(PropertyWhatever1 propertyWhatever1){
      this.propertyWhatever1 = propertyWhatever1
   }
   (..... more setters and getters, or whatevers.....)
}


In order to use your new singleton anywhere, do this:

MySingleton.getMySingleton().getPropertyWhatever1();

Note that you can do the same thing with pure static members and methods
without even making an instance. However using the true singleton model
gives you the flexibility for your methods and other called methods the
ability to access non static variables.  JBuilder's DataModules use the
singleton pattern as i've described here for example.

Luck,

Gerry


Rob Williams wrote in message <3593F482.6CAEEFBA@robco-inc.com>...
>I'm writing an app that needs to make some basic information available
>to a number of frames in the application, like the date that the current
>week began on, what location is using the program, etc.  Since we all
>know, obviously, that we aren't supposed to be doing public variables, I
>wonder if the right thing would be to create an instance of a class
>called AppSettings in the Application class and then get references to
>it in any of the frames where I need some of the information.  I tried
>to look at the Calendar class as an example of how to do this but didn't
>really learn anything; it seems to new a Gregorian Calendar in the
>constructor.
>
>Thanks,
>
>Rob Williams
>RobCo Incorporated
>
>
Technical Details
Message-ID: <6n319c$qf87@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3593F482.6CAEEFBA@robco-inc.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,b8537ec38763bb48
X-Google-Attributes: gidf74b3,public
From: "Gerry Mantha" <gmantha@home.com>
Subject: Re: Global Singleton Classes?
Date: 1998/06/27
Message-ID: <6n319c$qf87@forums.borland.com>
X-Deja-AN: 366599708
References: <3593F482.6CAEEFBA@robco-inc.com>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.1
Organization: QTECH Hybrid Systems Inc.
Newsgroups: borland.public.jbuilder.java.language