Inessence, thesingletonpatternisjust a wayofcreating a singleobjectthathassharedamongst a bunchofdifferentresourceisthroughoutyourapplicationwithouthavingtorecreatethatobjectorloseanyoftheinformationinsideofit.
Andallofourotherobjects A, B, C and D areallusingthatoneinstanceofthatsharedobjectthatisgoingtohaveallthestateforthatsingletoninsideofit, andtheydon't createtheirowninstanceatthatobjecteachtime.
Andthat's reallyallthesingletonpatternisis a waytocreate a singleobjectthathavesharedamongstthebusterdifferentparts, yourapplication.
Butthiscanleadtosomeproblemswithcreatingglobalvariables, SincetheSingleton's areessentiallyglobaltoyourentireapplication, having a singleobjectthatisglobaltoyourapplicationandcontrolsomuchofyourapplicationcouldbeincrediblyhardtotest, sinceyouneedtohavethatsingleobjectinordertotestanything, anditcancreate a bunchofcouplingbetweenthedifferentparts.
Using a singletonpatterninordertogetallthatinformationintoonecoherentplacecanbeeasiertoworkwiththanhavingtocreate a bunchofdifferentclassesandmakingsurethatthoseclassessavetheinformationproperlyandsoon.
Solet's diveintoanexampleofsomecodethatdoesnotusethesingletonpattern, which I believeshoulduse a singletonpatternandthenwhat's actuallyimplementthesingletondesignpatterninthatcoat.
Here I havevisualstudiocodeopenontheleftwithallthecodeforthisprojectandontheright.
I justhavetheconsolesowecanviewtheoutputofourcodebeforewegetstartedanyfurther.
I dowanttosayifyouarenotfamiliarwiththemodulesystemforexportingandimportingmodulesinJavaScript, makesuretocheckoutmyexportimportmoduleofvideo, whichwillbelinkedinthecardsandthedescriptionblow, becausewe'regoingtobeusingthatheavilyinthisexample.
Tostartwith, wehavethisfancylongerclasswhichallitdoesasyoucreate a newone, anditcreatesanemptyarrayoflogs, whichareallthethingsthatyouloginsideofit.
Ithas a functionforloggingoutanymessage.
It'lladdthatlogtothislogarraythatwecreated, anditalsologittothecouncilsothatwecanseeitandthenlastly, wehave a methodjusttoprintoutthenumberoflogsthatwehavesavedinsideofourclasssofar.
Andthenitgoesbacktohavingzerowogsinsideofourlongerandthenprintoutthesecondfileandsaysthatwehaveonelog, andthat's becauseineachofthesefileswe'recreating a newinstanceofthisfancylonger, sowedon't haveallthatsharedinformationfromthepreviousinstanceofthelongerbecausewe'recreating a newoneeverytimethatweimportitanduseit.
Thisis a greatusecaseforwherewe'regoingtowanttousetheSingletonpattern, sincewewantallusesofourfancylongerclasstoeactuallyhavethenumberoflogsinitthatwe'veloggedsofar.
Solet's gobacktoourfancylongerhereandimplementthisasifitwas a singleton.
Andsinceweonlywant a singleinstanceofthisclass, wewanttomakesurethatweonlyevercreateonesingleinstanceinourconstructor.
Soinordertodothat, wecouldjustuse a staticvariableonourfancylongersowecouldsayfancyloggerdotinstance, whichinthiscaseisjust a variablethatwe'recreating, andwewantthistoealwaysbe a singleinstanceofthefancylongerandnootherinstancecouldoverwritethis.
Soifthisinstanceisno, thatmeansthatwehavenotactuallycreated a fancylongeryet, sowewanttocreate a newone.
Andessentially, whatthisissayingisthatwealwayswanttoreturnthatsingleinstanceeverysingletimefromourconstructorinsteadofreturning a bunchofdifferentinstanceseverytimeandthendownherewewanttodoiswewanttocreatethatfancylongerinstancebeforeweexportitsowecouldjustcreate a variable, justcallnewfancylongerandnowwehaveanactualinstanceof R.