Re: Singleton Implementation Issue



Thanks Nicholas:

Why does [wanting to create a Singleton with a particular state upon
initialization] indicate in any way that the class should not be a
singleton?

My current motivation is as follows: At application startup I'm wanting to
create one instance of a class ("the singleton"). Many settings needed by
the singleton are stored in App.config. I am wanting to pass a
[System.Configuration.Configuration] to the singleton, rather than reading
the configuration settings directly from withing the singleton. One reason
that I don't want to read the configuration settings directly from within
the singleton is that I want to reuse the singleton in a Windows Forms app
(which uses App.config) and then in an ASP.NET Web version of the
application (which uses Web.config).

Thanks for your feedback!





"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:%23TK2ny60HHA.3848@xxxxxxxxxxxxxxxxxxxxxxx
Smithers,

If you are going to do that, then the singleton pattern doesn't really
apply. The whole point is that you have one instance here.

Now, having a property that returns that instance is fine (it doesn't
matter if it is a property or a method), but if the method takes
parameters, and the values passed to that method could change, then what
do you do the second time the method is called with different parameters?
Do you return the same instance?

It sounds like what you really want is something along the lines of a
class factory, which returns cached instances based on the parameters
passed in.

--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Smithers" <A@xxxxx> wrote in message
news:ufe1dS60HHA.5764@xxxxxxxxxxxxxxxxxxxxxxx
Unless I'm misunderstanding the pattern and it's various implementations,
Singleton effectively makes the constructor unavailable to clients. The
constructor for any given class, however, is what we otherwise use
[outside of Singleton implementations] to *require* specific parameters
be supplied upon instantiation (provided of course that we've removed the
default constructor).

So my question: I want to have a Singleton implementation that requires
specific parameters to be supplied upon instantiation.

My initial shot at a reasonable answer (please provide your thoughts on
this):
1. I'll go with one of the Singleton implementations that results in lazy
instantiation.
2. Then, in order to *require* the parameters to be supplied, I'll have a
*method* named Instance() instead of a *property* of that name. The
method then defines the arguments for which clients are to supply the
parameter values upon accessing the Singleton.

Thoughts? Considerations? Perspective?

Thanks.





.



Relevant Pages

  • Re: Singleton Implementation Issue
    ... Wanting to create a singleton with a particular state upon ... initialization doesn't mean it shouldn't be a singleton. ... Why does [wanting to create a Singleton with a particular state upon ... Singleton effectively makes the constructor unavailable ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Singleton Implementation Issue
    ... Singleton effectively makes the constructor unavailable to clients. ... I want to have a Singleton implementation that requires ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: My LogGenerator.java wont compile - too many errors - help
    ... I meant static factory method, it's what you implemented to replace the ... constructor that you made private. ... Consider the singleton pattern. ...
    (comp.lang.java.help)
  • Re: Singletons
    ... A singleton would usually have a private constructor to prevent ... but you're then breaking one of the rules of the singleton pattern. ... public function __construct ...
    (php.general)
  • Re: Singletons
    ... A singleton would usually have a private constructor to prevent ... but you're then breaking one of the rules of the singleton pattern. ... public function __construct ...
    (php.general)

Loading