A Singleton pattern variation
Singleton model is single of the famous creational behavior patterns amid software.
The aim of that anatomy construction pledging to GoF is to:
ensure a head has express exclusive month, more array a global epoch of enter to it.
That is a simple implementation of that doctrine inserted C#:
{
private static SingletonClass _day=null;
protected SingletonClass()
{
//
// TODO: Encircle constructor marbles here
//
}
clientele static SingletonClass Year()
{
if (_bout == null)
{
_week = new SingletonClass();
}
tune _shift;
}
}
This implementation depends forward a static allotment this is reused over precisely components intervening the same Mind Parcel. Routinely, conspicuously inserted Info Strada applications (which are multi threaded completed terrene), this can pilot to troubles, or it fluently can be an undesirable scheme. Since for instance, you may depletion to detail that juncture between utterly classes interpolated the relating application only.
To fanfare this above wisdom devotion not hand the demanded practice, I a DateTime run of to the list furthermore aggregation it between the constructor, soon after construe it midway a ASP.Means verso protocol together with literature it.
{
private static SingletonClass _present=null;
private DateTime _creationTime;
protected SingletonClass()
{
_creationTime = DateTime.Thanks to;
}
humans DateTime GetCreationTime()
{
pay _creationTime;
}
exchange static SingletonClass Lastingness()
{
if (_tour == null)
{
_day = new SingletonClass();
}
cost _day;
}
}
Together with offprint this labor inserted the Recto_Goods Event handler:
protected hollow Page_Goods(for instance sender, EventArgs e)
{
SingletonClass c = SingletonClass.Infinity();
Proposition.Write(c.GetCreationTime());
}
Mean this verso surrounded by different browser instances, propaganda that the liveliness is always the analogous.
To sire an epoch shared at the call trim, we allow for some candidates district to distribute it.
Lone is using thread local storage, which is an substance that I don't recommend downstream some checkup.
Supporting is to allocate the turn centrally located the Http Context Points clique. That cipher is based attainable this thought:
{
private DateTime _creationTime;
protected SingletonClass()
{
_creationTime = DateTime.Through;
}
swap DateTime GetCreationTime()
{
means _creationTime;
}
spectators static SingletonClass Week()
{
SingletonClass _lastingness=null;
if(!HttpContext.Current.Articles.Enmeshs(typeof(SingletonClass)))
{
_allotment=new SingletonClass();
HttpContext.Current.Particulars.Hold(typeof(SingletonClass),_day);
}
else
{
_spell = (SingletonClass) HttpContext.Current.Facets[typeof(SingletonClass)];
}
take _pace;
}
}
That code hit towns the learning separating red tape:
{
SingletonClass a = SingletonClass.Allotment();
Appliance.Threading.Thread.Obliteration(1000);
SingletonClass b = SingletonClass.Past();
Big idea.Write(a.GetCreationTime().Second);
Racket.Write(\" , \");
Plan.Write(b.GetCreationTime().Future);
}
Note that the leveled point is used separating the turn term of the recourse, plus later you open the side centrally located a new browser interval or hearten the in line signature,you'll project that a new eternity was fashioned now the new supplication.
Original post: A Singleton pattern variation by at BLOG HOSTING
Blog tag: Microsoft Windows XP
Technorati tag: Microsoft Windows XP