Singleton vs Static Methods

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm just getting into design patterns and am not sure I fully understand the
usefulness of the singleton. I know you can use it to ensure that you only
have one instance of a class, but why would you do that rather then just
using static methods on your class. Any input would be great.

Thank you,

Jake
 
I'm just getting into design patterns and am not sure I fully understand the
usefulness of the singleton. I know you can use it to ensure that you only
have one instance of a class, but why would you do that rather then just
using static methods on your class. Any input would be great.

One of the differences would be that a singleton object being an instance
of a class, it can be be passed around as a parameter of a method or
serialized... You can't pass a "static" class as a method parameter or
serialize it.
 
Hello Jake!

Singleton Pattern is used, if you want only one instance of your type.
There is no condition that your singleton object cant have a state
associated with it. Your singleton object might have properties
associated with it, which basically accounts for the state of the
object.

Just for the sake of example, you might want to implement the chess
board
as a singleton object or may be a cricekt ball object or more similarly
a
soccer ball.

State of the object means .. State of the chess board game i.e. where
are your horses located .. state of your cricekt ball i.e. which side
is the rough side (which accounts for the swing) .. colour of the
cricket ball (red/white .. white ball tends to swings more than
traditional red ball) .. how much is the ball seem tarnished. These all
things corresponds to the state of the object, which is singleton. How
would you go about keeping state with Static Methods.

Lets turn the table a little around and try considering the following
Factory example:

#region Singleton Behaviour

private static ConnectionFactory instance = new ConnectionFactory();

protected ConnectionFactory() { }

public static ConnectionFactory GetInstance() { return instance; }

#endregion

#region Factory Operation

public virtual IConnectionReader Create(String connectionType)
{
try
{
return (IConnection)
Activator.CreateInstance(Type.GetType(connectionType));
}
catch(Exception ex) { throw ex; }
}

#endregion

In the aforementioned example ConnectionFactory has a static method
"GetInstance()" and also an instance method "Create()"

The "static" method is used to get the instance of the factory class
and then after you get the instance you call the "instance
(non-static)" method to actually perform the factory operation.

I hope this might be of some help.

Let me know in case of any inconsistancy.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd
 
I'm just getting into design patterns and am not sure I fully understand
the
usefulness of the singleton. I know you can use it to ensure that you only
have one instance of a class, but why would you do that rather then just
using static methods on your class. Any input would be great.
This could be used as global variables.
Suppose you create a CGlobal class

Instead of copying the reference to every function you just instanciate the
CGlobal class wherever you need it and you have access to these global
defined variables. This way your code gets simplified because you do not
have to write additional code to pass on the CGlobal reference.

For example this is all you need to do:
private void ThisMethod() {
CGlobal Global=new CGlobal();
Global.InitialValue=true; // sets the global
variable
}

Static methods are mostly used when you wish to do some processing but you
do not need to have access to the class internal variables, so you might not
even want to instanciate the class. You could see this as global defined
procedures and functions declared outside the class when you look at C++.

For example you can use static method like this:
CGlobal Global=new CGlobal();
bool bTest=Global.StaticMethodTest("SomeTest"); // Like you would do
normally

or even shorter
bool bTest=CGlobal.StaticMethodTest("SomeTest"); // note, no NEW
used!

I hope this solves your question :-)
 
1. Difference in conceptual way ,static is procedural model of programming
and in OO,
everything is objects .So we need to stick to object in design,analysis and
implementation.
2. As mehdi said using singleton u can pass as parameters
3. Static methods are not polymorphic.
In Singleton, we can create several subclasses of original singleton class
and then choose at runtime which subclass was going to be instantiated and
then allow only one instances to be created.
 
These is also the factor that one day in the future you may decide to
make your sington multiple objects. It's a lot easier when it is
already a class
 
Back
Top