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 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 shakeela, one of the case you can use singleton pattern is to create a connection for your data base application.
satyaki mishra
-
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 shakeela, one of the case you can use singleton pattern is to create a connection for your data base application.
satyaki mishra
-
satyaki mishra wrote:
one of the case you can use singleton pattern is to create a connection for your data base application.
I don't believe that statement is particualrly helpful
And it's quite controversial use.
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
And it's quite controversial use.
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
dnh wrote:
And it's quite controversial use.
What use? His post is completely absent any use model, or any other contextual references, and therefore not helpful.
"one of the case you can use singleton pattern is to create a connection for your data base application." Ok, if I was asking about singletons this wouldn't help me. In that I agree with you. But I believe that he is talking about scenario when you have class that manages connection to db, and creates connection in constructor. If that class is singleton, only one instance is created, so only one connection is created (which is usually very expensive operation). But then there's a pooling and you will have connection opened for whole lifetime of the singleton object, sometime you'll connect to db even when you don't need to etc. Which is why I say this is not the supercleanest use of singleton pattern.
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
"one of the case you can use singleton pattern is to create a connection for your data base application." Ok, if I was asking about singletons this wouldn't help me. In that I agree with you. But I believe that he is talking about scenario when you have class that manages connection to db, and creates connection in constructor. If that class is singleton, only one instance is created, so only one connection is created (which is usually very expensive operation). But then there's a pooling and you will have connection opened for whole lifetime of the singleton object, sometime you'll connect to db even when you don't need to etc. Which is why I say this is not the supercleanest use of singleton pattern.
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfednh wrote:
Which is why I say this is not the supercleanest use of singleton pattern.
It's a perfectly fine design when the use model is a single user desktop application connecting to a desktop database. Your post also lacks specific use model information required to draw any conclusion as to the accuracy of your statement.
-
dnh wrote:
Which is why I say this is not the supercleanest use of singleton pattern.
It's a perfectly fine design when the use model is a single user desktop application connecting to a desktop database. Your post also lacks specific use model information required to draw any conclusion as to the accuracy of your statement.
led mike wrote:
It's a perfectly fine design when
Isn't that little word "when", and what follows it, proving my point? (but then again, I've just came from work :zzz:)
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
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,
shakeela wrote:
I want to know in which scenario we are using singleton pattern
When dumped by ones girlfriend or significant other.
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
-
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,
shakeela wrote:
I want to know in which scenario we are using singleton pattern in our appl
Here is one scenario[^] But, my plans for this series will remove the singleton pattern later.
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
-
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,
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 ------>
-
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.