|
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
|
|
|
|
|
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
|
|
|
|
|
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 )
[ 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
|
|
|
|
|
shakeela wrote: i want applicable knowledge in application development.
That is "experience" and we can't give that to you.
|
|
|
|
|
shakeela wrote: I want to know in which scenario we are using singleton pattern
When dumped by ones girlfriend or significant other.
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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 ------>
|
|
|
|
|
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 ------>
|
|
|
|
|
|
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.
|
|
|
|
|
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 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
|
|
|
|
|
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:
<br />
public sealed class SingletonCreator<Type> where Type : class, new()<br />
{<br />
<br />
#region Private declarations<br />
private static Type instance;<br />
#endregion<br />
<br />
#region Properties<br />
public static Type GetInstance()<br />
{<br />
lock (typeof(Type))<br />
{<br />
if (instance == null)<br />
{<br />
instance = new Type();<br />
}<br />
return instance;<br />
}<br />
}<br />
#endregion<br />
<br />
}<br />
Usage:
<br />
MyClass myClass = SingletonCreator<MyClass>.GetInstance();<br />
I hope this helps you in your quest to learn design patterns Have an awesome weekend...
Fernando Mendes
Senior .NET Developer, Architect
|
|
|
|
|
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
|
|
|
|
|
Hi,
I need to develop three applications over LAN. One is the core application, written in C++ and the other two are clients application.
The two clients application are GUI for the core application, they don't run on the same machine.
Can you advise me what kind of technology to use for communication between them?
I prefer to write the GUI application in C# or V.B .Net
Thanks
Yoav Zur
|
|
|
|
|
|
Hello,
I'm building a base class to hold information taken from a robot movement file.
This means I have several lines, each with a set of points, and sometimes commands, between the points. Each point has some data (coordinates, orientation, zone, speed, work object, and a few more).
I'm using Eran Kampf's Sharp3D math library.
I would like a sugestion on how to implement the base class.
So far, I've done the following:
using Sharp3D.Math.Core
using Sharp3D.Math.Geometry3D
namespace ModelClass
{
#region Class constants
public enum Linetypes
{
undefined = -1,
laser = 0,
water = 1,
glue = 2,
primer = 3,
rough = 4
}
public enum Polysides
{
undefined = -5,
none = -1,
right = 0,
left = 1
}
#endregion
public class Point7D
{
// Represents a point in 3D space
Vector3F point;
// Represents the quaternion of vector associated with point
QuaternionD quaternion;
// Point name descriptor
string name;
}
(...)
public class RobotPoint : Point7D
{
#region Constants
public enum zones
{
undefined,
fine,
z0,
z1,
z5
}
public enum speeds
{
undefined,
v5,
v10,
v100,
v1000
}
const string ActiveTool = "ActiveTool";
const string AuxPointData = "[0,0,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]]";
#endregion
#region Internal fields
// Workplace definition
string workplace;
// Tool defined for the point
string tool;
// Zone definition
zones zone;
// Speed definition
speeds speed;
#endregion
}
public class PolyLine7D
{
#region Internal Fields
List<RobotPoint> pointList;
string name;
Linetypes type;
int width;
int npasses;
int npts;
int power;
Polysides polyside;
#endregion
}
public class Model
{
#region Internal Fields
double front_tolerance;
double back_tolerance;
double inside_x_sep;
double outside_x_sep;
double plan_overlap;
List<PolyLine7D> _ModelPolylines;
string _ModelFileName;
#endregion
}
}
Should the class PolyLine implement itself a static method for creating a list, instead of having the Model class do it?
Same question for the Model class if I want to have more than one file opened at the same time? In such case, I would create a Hashtable, with the _ModelFileName as a key.
Thanks for any help you can give me.
|
|
|
|
|
Have you come up with any good ideas?
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|