G
Guest
Hi All,
What is the diff. between a singleton class and a static class in C#?
What is the diff. between a singleton class and a static class in C#?
DBA said:What is the diff. between a singleton class and a static class in C#?
first the currently there is no a way to declare a class static
Nicholas Paldino said:Ignacio,
The statement:
first the currently there is no a way to declare a class static
Is not true. In the current beta release of C# 2.0, there is support
for static classes, where all of the members must be static (and the
compiler issues a warning if any of the members are not).
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Ignacio Machin ( .NET/ C# MVP ) said:Hi,
first the currently there is no a way to declare a class static, you do
it implicitely by declaring static all the members of it. The members
will exist as long as the Appdomain does.
A singleton is a "normal" class with the only difference that the
constructor is private, hence the only way to "create" an instance is
using the property/method that return the instance. If needed you can
also declare a method that dispose the instance, so it can be recreated
again when needed.
cheers,
KH, Jon Thank you for responses. One more question:
When to use a singleton class and when to use a staic class? I think
both has the same functionality
Jeff Louie said:First, the Singleton Pattern generally creates a single instance of a
class, but this is not absolute. In fact, you can argue that some of
the flexibility of the Singleton Pattern is that it allows you the
programmer to return more than one instance of a class at a later
date without breaking the client code.
There _are_ advantages to the Singleton pattern and they include:
3) Allows sub-classing.
It allows the singleton to be a subclass of something else. The normal
patterns that ensure that only a single instance is ever created don't
work well with subclasses. It can be done, but it's somewhat messy.
dot.state.fl.us> said:Why messy?
It's the member who return the instance the one in charge of that, it could
work as a factory deciding at runtime an instance of what class to create
and return. Somewhere I saw a code like this:
public static SingletonClass GetInstance()
{
if ( instance == null )
{
//decide which class derived of SingletonClass to instantiate
return instance
}
}
oneCertainly the GoF book defines a singleton in terms of only providing
patterns that ensure that only a single instance is ever created don'tIt allows the singleton to be a subclass of something else. The normal
Jeff Louie said:patterns that ensure that only a single instance is ever created don't
work well with subclasses. It can be done, but it's somewhat messy.<
Hmm. I don't understand why this is messy. In C# an interface can be
looked at as subclassing where a concrete class implements an
interface or extends a pure virtual class. The singleton GetInstance
method can return an interface type or a method can take a singleton
reference of an interface type allowing you to program to an
interface. The supplier supplies a single instance of an object that
implements the contract, the interface.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.