Re: When is it appropriate to use static methods?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 05/11/04


Date: Tue, 11 May 2004 19:55:16 +0100

Tom <nfr@nospam.com> wrote:
> "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
> news:MPG.1b0a94d2e14888a298a89f@msnews.microsoft.com...
> > Tom <nfr@nospam.com> wrote:
> >
> > If your code even *allows* you to create a new instance each time, it's
> > no longer a singleton, by definition.
>
> No. I am using the following format to create my singleton:
>
> namespace Company.TWS.BusinessObject
> {
> public class SummaryBO
> {
> private static TwsDS _twsDS;
> private static string xmlLocation = @"c:\data\tws\tws.xml";
> private SummaryBO() {}
> public static readonly SummaryBO Instance = new SummaryBO();
> public static TwsDS twsDS { get { return _twsDS; } }
>
> [...]
> }
> }

I'm not sure why you've got an instance at all, if everything's static.
The normal singleton pattern would have the _twsDS and twsDS
field/property as an instance, and you'd refer to it using

SummaryBO.Instance.TwsDS

That *doesn't* create an instance each time.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Can use of singletons denote poor project design?
    ... Changes the format to something other than what is required for the ... To see if the target function respects the date format! ... singleton (because, gee, time is a universal constant, so there's only ... Who said anything about the DateFormat being a singleton? ...
    (comp.object)
  • Re: Suggest improvements
    ... > could print out every array element as a singleton, ... How long does the above loop take, ... So the 90MB refers to the input XML in text format? ...
    (comp.lang.c)
  • Re: Can use of singletons denote poor project design?
    ... returns a default date format, which is a singleton object, there is ... Could you outline a test that changes the format, ... I implied my method can illustrate why some of them suck. ...
    (comp.object)
  • Re: Can use of singletons denote poor project design?
    ... relies on formatted dates, and test its output is correct? ... Changes the format to something other than what is required for the ... To see if the target function respects the date format! ... singleton (because, gee, time is a universal constant, so there's only ...
    (comp.object)
  • Re: Can use of singletons denote poor project design?
    ... returns a default date format, which is a singleton object, there is ... one instance of a default date format. ... Presumably the default format depends on the default locale, ...
    (comp.object)