A singleton stores common data in only on place. A static
class is also used to store single-instance data. We save state between usages.
We store caches to improve performance. The object must be initialized only
once and shared.
Singleton: when
we create a singleton class using singleton design pattern try to write that
class in a way that there could be only one instance. Below is the code which
show how to create a singleton class
using System
public sealed class Singleton
{
private
static volatile Singleton _instance;
private
static object syncRoot = new object();
private
Singleton(){}
public
static Singleton Instance
{
get
{
if(_instance ==
null)
{
Lock(syncRoot)
{
if(_instance == null)
if(_instance == null)
{
_instance
= new Singleton();
}
}
}
return
_instance;
}
}
}
Notes:-
1.
This approach ensures that only one instance is
created and only when the instance is needed.
2.
The volatile keyword indicates that a
field might be modified by multiple threads that are executing at the same
time. Fields that are declared volatile are not subject to compiler
optimizations that assume access by a single thread. This ensures that the most
up-to-date value is present in the field at all times.
3.
This double-check locking approach solves the
thread concurrency problems while avoiding an exclusive lock in every call to
the Instance property method. It also allows you to delay
instantiation until the object is first accessed.
4.
Making this singleton class sealed will avoid
any chance of inheriting this class and making further complicated.
Million Dollar Question:
- So what Benefits we get in a singleton class
over a static class?
Answer :- Both singleton
class and a static classes are used to have only one instance of that class but
there are few benefits which singleton class provides over static class.
1.
Singleton class is lazy load means it load only
when it’s required. When we ask for the instance from a static class it will
check if there is no instance created it will create it and return the instance
but static class object are created and loaded when application starts. Which make
application starts slow.
2.
You can implement an interface or derive you
Singleton Class from a base class but you can't do the same with static class.
3.
You can have non static in singleton class but
your static class should have only static members.
4.
You can pass a singleton class as a parameter to
a method but the same you can’t do with static class.
5.
In a static class CLR makes sure there is only
one instance of static class, but in case of singleton class we need to write
in a way we make sure there is only one instance of singleton class.
Conclusion: - Basically
speaking, they are meant for different purposes. A static class can be
used as a convenient container for sets of methods that just operate on input
parameters and do not have to get or set any internal instance fields. A singleton
class can be used when you need a class with all the benefit of a normal class
and an additional benefit will be that there will be only one instance of this
class.
Comments
Post a Comment