Singleton Pattern with non-default constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom

    Singleton Pattern with non-default constructor

    In a web-application I need to read some configuration from a database.
    I like to write a configuration class which will be implemented as
    thread-safe singleton.

    I connect through ODBC to the database. So to be able to do that I need
    to pass the Datasource Name.

    How can I create a singleton that has a constructor with a parameter?
    Standard singleton pattern always use the default constructor. To
    provide an init(string datasource_name ) method is no real fun, since the
    state of the singleton instance needed to be checked whenever used.

    Is there any better solution to this?

    Tom
  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Singleton Pattern with non-default constructor

    Tom wrote:
    In a web-application I need to read some configuration from a database.
    I like to write a configuration class which will be implemented as
    thread-safe singleton.
    >
    I connect through ODBC to the database. So to be able to do that I need
    to pass the Datasource Name.
    >
    How can I create a singleton that has a constructor with a parameter?
    Standard singleton pattern always use the default constructor. To
    provide an init(string datasource_name ) method is no real fun, since the
    state of the singleton instance needed to be checked whenever used.
    >
    Is there any better solution to this?
    From the philosophical point of view you could arhue that if the
    parameter is not fixed, then it is not really a singleton.

    From the practical point of view you would probably just have the
    constructor read the name from a config file.

    The alternative would be a multipleton, where the GetInstance
    method has a string argument and the static instance field
    is a Dictionary<stri ng,Xand the GetInstance lookup and create
    if necessary.

    Arne

    Comment

    • Bob Powell [MVP]

      #3
      Re: Singleton Pattern with non-default constructor

      If the object returned is state-dependent on the constructor parameter then
      this is not a good target for the singleton pattern.

      Effectively, the singleton objects state will change for all objects that
      have a reference to it.

      In this case, and if you really want to use a singleton, a more correct
      method would be to maintain the pattern as described by the pattern and
      provide a method to set the state.

      --
      --
      Bob Powell [MVP]
      Visual C#, System.Drawing

      Ramuseco Limited .NET consulting


      Find great Windows Forms articles in Windows Forms Tips and Tricks


      Answer those GDI+ questions with the GDI+ FAQ


      All new articles provide code in C# and VB.NET.
      Subscribe to the RSS feeds provided and never miss a new article.


      "Tom" <dev@bednarz.ch wrote in message
      news:ulPu5Fg2IH A.3968@TK2MSFTN GP04.phx.gbl...
      In a web-application I need to read some configuration from a database. I
      like to write a configuration class which will be implemented as
      thread-safe singleton.
      >
      I connect through ODBC to the database. So to be able to do that I need to
      pass the Datasource Name.
      >
      How can I create a singleton that has a constructor with a parameter?
      Standard singleton pattern always use the default constructor. To provide
      an init(string datasource_name ) method is no real fun, since the state of
      the singleton instance needed to be checked whenever used.
      >
      Is there any better solution to this?
      >
      Tom

      Comment

      Working...