Re: Implementation of Singleton

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



Hi,

Thanks for all the input but it has now gone way over my head. I only
have a basic understanding of classes and was hoping that I would be
able to learn as I went along.

I have already seen the webpage recommended but this doesn't tell you
how to use a Singleton in practice, just how to create it. Maybe I
just don't have the experience yet.

I initially wanted to use a global variable to hold the location of
the log file. This is simple as you set its value once and then use
it wherever you want. As the value will never change there is very
little risk of the set value becoming corrupt.

I know that the general concensus is that global variables are 'bad'
so I looked on the internet and discovered Singletons were a suggested
option. This is why I have arrived at this point. Why are global
variables considered 'bad', surely they have a place. Why add loads
of extra code that needs executing when it isn't needed?

Your thoughts and advice are really appreciated, please help further.

Many thanks,

Tony W

On Wed, 01 Jun 2005 16:40:21 +1000, John B <jbngspam@xxxxxxxxx> wrote:

>Jon Skeet [C# MVP] wrote:
>> John B <jbngspam@xxxxxxxxx> wrote:
>>
>>> >> public static DebugLog Instance
>>> >> {
>>> >> get
>>> >> {
>>>
>>>if(m_Instance == null)
>>>{
>>> m_Instance = new DebugLog();
>>>//Question Jon, can you set a readonly field like this? Havent tried, no
>>>time at the mo, will try later.
>>>}
>>
>>
>> No - but the above isn't thread-safe anyway :) (You'd need to lock on
>> something for the duration of the property call.)
>
>I know it isnt thread safe ;)
>thanks to some initial reading and thinking re your singleton page.
>>
>>
>>> >> return m_Instance;
>>> >> }
>>> >> }
>>>
>>>If this does not work, or just because you want to, use:
>>>private static readonly DebugLog m_Instance = new DebugLog();
>>
>>
>> That's much better - you could even just make it
>>
>> public static readonly DebugLog Instance = new DebugLog();
>>
>> and get rid of the property entirely, if you're convinced you'll never
>> want to do anything else.
>>
>
>Im never convinced that Ill never want to do anything else ;)
>
>Thanks for the input
>JB

.



Relevant Pages

  • Re: approach to writing functions
    ... > and it's written like a shell script. ... They complain about global variables, ... bite-sized bits you can keep in your head but rather in one long stream. ...
    (comp.lang.python)
  • Re: Singletons
    ... Besides the point that global variables are bad, ... we could do this with a Singleton or we can rearrange ... > IO streams, ... > default stdout" would return the standard output stream. ...
    (comp.object)
  • Re: Whats the best way to define global variable
    ... > can not understand the point put forward by Karl regarding "singleton surfes ... The purpose of a singleton is *not* to be a replacement of global variables. ... Karl Heinz Buchegger ...
    (comp.lang.cpp)
  • Re: Singleton Pattern
    ... but I don't think there's an ounce of truth in it. ... One practical problem with global variables (considered as a technology ... One solution to that is to use something like the Singleton pattern (or even ... what is a DBMS if it is not a chunk of data available to ...
    (comp.lang.java.programmer)
  • Re: Implementation of Singleton
    ... I know that the general concensus is that global variables are 'bad' ... and I will email you a zipped working sample of a singleton and its usage if that will help. ... Its always easier to "see" a working sample. ... private static readonly DebugLog m_Instance = new DebugLog; ...
    (microsoft.public.dotnet.languages.csharp)