locked
Wants to know when should i use singleton pattern RRS feed

  • Question

  • User1182241144 posted

    Please provide me practical example.

    Friday, March 1, 2013 7:07 AM

Answers

  • User371383779 posted

    Singleton pattern is used where only one instance or only one object is needed and a global point of access is needed to it. Basically it caters to a requirement of restricting instance of an object to only one, when you want that a particular class has its one instance at any point of time.

    One practical scenario is where i use it, for a global validator. In a web application i have this validator that validates user credentials against a configured Database. So it does not matter how many users login the web application only one instance of the validator validates all the users.

    Another usage could be a Log Writer, Auditer, Tracer etc.

    For more details, have a look at the following articles -

    http://msdn.microsoft.com/en-us/library/ff650316.aspx

    http://www.dofactory.com/Patterns/PatternSingleton.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 1, 2013 8:28 AM

All replies

  • User-760709272 posted

    You'll use it when you only want one instance of something.  For example a class that contains configuration details, or maybe you have a class that manages a collection of data that is universal like a list of countries or whatever.

    Friday, March 1, 2013 8:25 AM
  • User371383779 posted

    Singleton pattern is used where only one instance or only one object is needed and a global point of access is needed to it. Basically it caters to a requirement of restricting instance of an object to only one, when you want that a particular class has its one instance at any point of time.

    One practical scenario is where i use it, for a global validator. In a web application i have this validator that validates user credentials against a configured Database. So it does not matter how many users login the web application only one instance of the validator validates all the users.

    Another usage could be a Log Writer, Auditer, Tracer etc.

    For more details, have a look at the following articles -

    http://msdn.microsoft.com/en-us/library/ff650316.aspx

    http://www.dofactory.com/Patterns/PatternSingleton.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 1, 2013 8:28 AM
  • User-1156041913 posted

    Singleton pattern is used where only one instance or only one object is needed and a global point of access is needed to it. Basically it caters to a requirement of restricting instance of an object to only one, when you want that a particular class has its one instance at any point of time.

    One practical scenario is where i use it, for a global validator. In a web application i have this validator that validates user credentials against a configured Database. So it does not matter how many users login the web application only one instance of the validator validates all the users.

    Another usage could be a Log Writer, Auditer, Tracer etc.

    For more details, have a look at the following articles -

    http://msdn.microsoft.com/en-us/library/ff650316.aspx

    http://www.dofactory.com/Patterns/PatternSingleton.aspx

    Pankaj can you PLEASE also explain in which layer singleton will be used and what wold be architecute. In practicle which layer will call it and in which layer it will be residing and responding.

    Thanks a lot for you help and guidance.

    Tuesday, June 25, 2013 7:59 AM