473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Singleton vs Static Methods

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

Oct 25 '05 #1
5 5588
On Tue, 25 Oct 2005 01:16:03 -0700, Jake wrote:
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.
Oct 25 '05 #2
MUS
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(connectionTy pe));
}
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

Oct 25 '05 #3
> 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 :-)


Oct 25 '05 #4
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.

"Mehdi" wrote:
On Tue, 25 Oct 2005 01:16:03 -0700, Jake wrote:
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.

Oct 25 '05 #5
Neo
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

Oct 25 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Powell | last post by:
Consider this: class ActionHandler { ...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
6
by: VidalSasoon | last post by:
I have a singleton class that I want to only contain a hashtable. I want to be able to modify this hashtable at will. The problem I am having is each time I try to update the data using the...
15
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
5
by: Bob | last post by:
Does anyone know of any adverse affects of using singleton data access objects in an ASP.NET Web Service? I've been forced to implement it this way but it just doesn't seem right to me. I haven't...
1
by: Diffident | last post by:
Guys, I have been cracking my head over this concept in .NET framework. I have read many posts on this topic but not clear about this and hence I am posting it again. If you have designed...
5
by: Diffident | last post by:
Hello All, I am designing a class based on singleton pattern. Inside this class I have multiple instance methods. My question is since there will be only one instance of this class at any...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.