singleton pattern example
-
hi everybody, Iam new to design pattern concept , I want to know in which scenario we are using singleton pattern in our appl. I need exact scenario because i had some examples and gone the theory in books i want applicable knowledge in application development. Thanks in advance for replay,
Hi Dear as theory says singleton use is for making one instance available throughout the life time of application As far as scenario is concerned the dbconnection is bad one as some one said becos of pooling. You can think of salary being credited and debited to employee accounts in banks. you can make sure this operation is implemented using singleton class can also make sure only the manager or single person in bank has access to this class.
Gokul- MCSD:MCAD:MCP - Technical Lead - GFT INDIA
-
I consider singletons as evil! If there has to exist really one one instance of a class then I use a manager pattern (don't know if this patter has a real pattern name): I request the "singleton" from a Manager that will return me the instance. Depending on configuration and other things it will return me a singleton or a new instance. Furthermore I request interfaces which allows the replacement of "singletons" either for testing (mocks) or changed requirements or if it is a base functionality that is used in different projects. Much more flexible.
-^-^-^-^-^- no risk no funk ................... please vote ------>
Sounds like you are using the Factory Pattern to generate the singleton.
Urs Enzler wrote:
Furthermore I request interfaces which allows the replacement of "singletons" either for testing (mocks)
Very useful for that.
Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
-
Sounds like you are using the Factory Pattern to generate the singleton.
Urs Enzler wrote:
Furthermore I request interfaces which allows the replacement of "singletons" either for testing (mocks)
Very useful for that.
Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
I'm not sure what the exact name is, because a Factory is something that instanciates objects for me, but a manager does that and manages the instance. If I request the same interface I'll get the same instance.
-^-^-^-^-^- no risk no funk ................... please vote ------>
-
I consider singletons as evil! If there has to exist really one one instance of a class then I use a manager pattern (don't know if this patter has a real pattern name): I request the "singleton" from a Manager that will return me the instance. Depending on configuration and other things it will return me a singleton or a new instance. Furthermore I request interfaces which allows the replacement of "singletons" either for testing (mocks) or changed requirements or if it is a base functionality that is used in different projects. Much more flexible.
-^-^-^-^-^- no risk no funk ................... please vote ------>
Urs Enzler wrote:
I consider singletons as evil!
It's just a "Creational Pattern", there are many, your situation calls for a different one. That does not provide reasoning for declaring the Singleton to be "Evil". However there are plenty of arguments about it on the WikiWikiWeb.
-
Urs Enzler wrote:
I consider singletons as evil!
It's just a "Creational Pattern", there are many, your situation calls for a different one. That does not provide reasoning for declaring the Singleton to be "Evil". However there are plenty of arguments about it on the WikiWikiWeb.
Yes of course, I'm a bit exaggerating :) The main reason I'm not a fan of the singleton pattern is because it is overused. 1) The decision that something exists only once in an application is a very final one. Normally singletons are very basic things in an application (a logger for example). And if that changes (happend about three times to a project I was working in) it is very hard to refactor (because of point 2) 2) It is often the case that projects using singletons are not loosly coupled: components reference the singleton by something like MySingletonClass.Instance. I consider this very limiting. In an earlier post I mentioned that I use a Manager pattern (or Factory pattern) that allows me to exchagne the real object behind the "singleton" for example for testing (mocks). 3) In a framework or in a base components library, you should definitly not use singletons because they can not be replaced by project specific instances. Because of these reasons I prefer using a manager pattern (Factory pattern) because it is not really more complicated but very much more flexible and not limited. And as a final word: yes there are situations where the singleton pattern is adequat, but they are seldom in my eyes or there exists a better pattern for the scenario.
-^-^-^-^-^- no risk no funk ................... please vote ------>
-
Yes of course, I'm a bit exaggerating :) The main reason I'm not a fan of the singleton pattern is because it is overused. 1) The decision that something exists only once in an application is a very final one. Normally singletons are very basic things in an application (a logger for example). And if that changes (happend about three times to a project I was working in) it is very hard to refactor (because of point 2) 2) It is often the case that projects using singletons are not loosly coupled: components reference the singleton by something like MySingletonClass.Instance. I consider this very limiting. In an earlier post I mentioned that I use a Manager pattern (or Factory pattern) that allows me to exchagne the real object behind the "singleton" for example for testing (mocks). 3) In a framework or in a base components library, you should definitly not use singletons because they can not be replaced by project specific instances. Because of these reasons I prefer using a manager pattern (Factory pattern) because it is not really more complicated but very much more flexible and not limited. And as a final word: yes there are situations where the singleton pattern is adequat, but they are seldom in my eyes or there exists a better pattern for the scenario.
-^-^-^-^-^- no risk no funk ................... please vote ------>
Urs Enzler wrote:
The main reason I'm not a fan of the singleton pattern is because it is overused.
:laugh::laugh::laugh: not laughing at you. It made me think, well duh software development is overused misused, and pretty much everything in it is misused. :-D You should see how some of my peers abused the word "Agile" :laugh::laugh::laugh: :-D :) :| :( :^) :((:((:((
Urs Enzler wrote:
Because of these reasons I prefer using a manager pattern (Factory pattern)
You probably know this but there are many more creational patterns[^]
-
Urs Enzler wrote:
The main reason I'm not a fan of the singleton pattern is because it is overused.
:laugh::laugh::laugh: not laughing at you. It made me think, well duh software development is overused misused, and pretty much everything in it is misused. :-D You should see how some of my peers abused the word "Agile" :laugh::laugh::laugh: :-D :) :| :( :^) :((:((:((
Urs Enzler wrote:
Because of these reasons I prefer using a manager pattern (Factory pattern)
You probably know this but there are many more creational patterns[^]
led mike wrote:
You probably know this but there are many more creational patterns[^]
Unfortunately, this page does not mention the kind of pattern we are using. Maybe this is because it is actually a Factory (instance creation) but with managing functionality :) May the crusade for good design continue ...
-^-^-^-^-^- no risk no funk ................... please vote ------>
-
led mike wrote:
You probably know this but there are many more creational patterns[^]
Unfortunately, this page does not mention the kind of pattern we are using. Maybe this is because it is actually a Factory (instance creation) but with managing functionality :) May the crusade for good design continue ...
-^-^-^-^-^- no risk no funk ................... please vote ------>
Urs Enzler wrote:
but with managing functionality
"managing" would be structural.
Urs Enzler wrote:
Depending on configuration and other things it will return me a singleton or a new instance.
It is difficult to understand that with any certainty. Perhaps you are using both Factory and Flyweight[^] Patterns. Or perhaps you are referring to a higher level abstraction of both into a Composite or Facade. I can't tell.
-
Urs Enzler wrote:
but with managing functionality
"managing" would be structural.
Urs Enzler wrote:
Depending on configuration and other things it will return me a singleton or a new instance.
It is difficult to understand that with any certainty. Perhaps you are using both Factory and Flyweight[^] Patterns. Or perhaps you are referring to a higher level abstraction of both into a Composite or Facade. I can't tell.
Hmm, I'd say it is as you say a higher level of abstraction and containing several individual design patterns. I think I should write an article about that, that would make the discussion easier ;)
-^-^-^-^-^- no risk no funk ................... please vote ------>
-
hi everybody, Iam new to design pattern concept , I want to know in which scenario we are using singleton pattern in our appl. I need exact scenario because i had some examples and gone the theory in books i want applicable knowledge in application development. Thanks in advance for replay,
Greetings Shakeela, The Singleton design pattern serves to describe an implementation that restricts instantiation of a class to one object, in other words no copies or additional references. I've created a Singleton instantiate for you here to experiment with and get an idea of my description above. the singleton design pattern also belongs to the Creational Patterns group which consist of the following additional design patterns: o Abstract factory pattern: centralize decision of what factory to instantiate o Factory method pattern: centralize creation of an object of a specific type choosing one of several implementations o Builder pattern: separate the construction of a complex object from its representation so that the same construction process can create different representations o Lazy initialization pattern: tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed o Object pool: avoid expensive acquisition and release of resources by recycling objects that are no longer in use o Prototype pattern: used when the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) is prohibitively expensive for a given application The singleton class you can experiment with:
/// Any type with a public and parameterless constructor. public sealed class SingletonCreator where Type : class, new() { #region Private declarations /// /// The instance to retain after creation /// private static Type instance; #endregion #region Properties /// /// A lazy instance instantiator property. It will test for the existence of an object and return that reference otherwise it will create a new object. /// /// An singleton instance of type "Type". public static Type GetInstance() { lock (typeof(Type)) { if (instance == null) { instance = new Type(); } return instance; } } #endregion }
Usage:// This will always create an instance it it doesn’t exist or return the // existin reference if it was previously called. MyClass myClass = SingletonCreator<MyClass>.GetInstance();
I hope this helps you in your quest to learn design patterns ;) Have an awesome weekend...Fernando Mendes Senior .NET Developer, Architect
-
hi everybody, Iam new to design pattern concept , I want to know in which scenario we are using singleton pattern in our appl. I need exact scenario because i had some examples and gone the theory in books i want applicable knowledge in application development. Thanks in advance for replay,
singletons are used when it only makes sense to have exactly one and only one of them... i am not sure the impact singletons have on multi-threaded apps either, you might want to do some research in that area as well. design patterns are an interesting concept... i always stick to the kiss principle.. and avoid over-engineering my code. sometimes you can get into trouble with singletons, because sometimes they are implemented with private a constructor and destructor, and some example implementations you find on the web have nearly undetectable flaws ... there are variations to this theme between java and c++, and i am not sure about python (or whether you even need to do it in python). be cautious and judicious in your decision making, kind regards,
David