none
Singleton pattern with Overloaded constructors RRS feed

  • Question

  • Hi,

    How do you use Singleton pattern with Overloaded constructors means , i need to initialize various properties and then instantiate object accordingly

     

    Tuesday, June 12, 2007 6:45 AM

Answers

  • The singleton pattern typically hides the constructor from callers because you don't want to allow callers to create multiple instances.

     

    Initialization with different parameters presents an interesting issue.  You would want to insure that once initialized only one of the singleton objects would exist.  What would happen if someone tried to initialize with different parameters?

     

    If the behavior varies depending upon these parameters just for clarity sake you might want to consider creating seperate classes.  This results in a clearer understanding from the consumers point of view. 

     

    For example... suppose that the Singleton in scenario A is initialized with value X resulting in behavior Y.  Everyone who calls this sinngleton during it's lifetime now gets behavior Y.

     

    In another session, the Singleton in scenario B is initialized with value Q resulting in behavior Z.  Now everyone who calls this singleton gets a different behavior.

     

    This sounds like a recipe for confusion.  Instead, you could create SingletonWithBehaviorY and SingletonWithBehaviorZ.  Now the expectation is much clearer.

    Wednesday, June 13, 2007 9:59 PM

All replies

  • The singleton pattern typically hides the constructor from callers because you don't want to allow callers to create multiple instances.

     

    Initialization with different parameters presents an interesting issue.  You would want to insure that once initialized only one of the singleton objects would exist.  What would happen if someone tried to initialize with different parameters?

     

    If the behavior varies depending upon these parameters just for clarity sake you might want to consider creating seperate classes.  This results in a clearer understanding from the consumers point of view. 

     

    For example... suppose that the Singleton in scenario A is initialized with value X resulting in behavior Y.  Everyone who calls this sinngleton during it's lifetime now gets behavior Y.

     

    In another session, the Singleton in scenario B is initialized with value Q resulting in behavior Z.  Now everyone who calls this singleton gets a different behavior.

     

    This sounds like a recipe for confusion.  Instead, you could create SingletonWithBehaviorY and SingletonWithBehaviorZ.  Now the expectation is much clearer.

    Wednesday, June 13, 2007 9:59 PM
  • In singleton pattern you control instantiation, but you are free to use a non-default or overloaded constructor. You are free to choose you own instance creating strategy.  

     

    If you post the Singleton implementation code you are using,  we can try to come up a solution for you.

     

     

    Singleton: “Ensure a class has only one instance and provide a global point of access to it” [GOF]

    Wednesday, June 13, 2007 10:14 PM
  • Well what Im trying to do now is setup and instantiate a class such that the GC wont remove it, and am thinking of using the singleton so that the .net framework wont remove the class if its somehow out of scope or its lease expired(?) then create a new instance thereby filling up new/extra references until its bogged down and couldnt run anymore.

     

    I want to make a class, instantiate it, then padlock the thing so the GC wont take it away.  I used to think the new GC or memory management was great until having to deal with resource leaks, now embedded doesnt seem so archaic anymore.

     

    How about having an "embedded" keyword so that the system will treat certain code as if its embedded, then the gremlins wont get in and delete things invisibly in the background, giving no clues except a loss of resources until the system runs 1000x slower

     

    So would the singleton pattern help protect against resource leaks?

    Thursday, July 5, 2007 6:58 PM
  • I may have the wrong end of the stick here, but I think you want a Factory to return your specific singleton instance?

    Thursday, July 5, 2007 10:54 PM
  • I're probably using Activator.CreateInstance to create an instance of some class, rigth?

     

    You have a problem when this classes implement some interface or base class and you can't know what constructors they have.

     

    The way I do it is documenting the interfaces or base classes with the constructor signature implementers must implement and and assume that constructor exists. If it doesn't, then I don't know how to create an instance.

    Thursday, July 5, 2007 11:52 PM