Soif I dividebythree, forexample, youseeitdividesbythreeoverhere, andrightnowthisisnotusingthecommandpattern.
Thisisjust a basicjobscriptclassthatyou'reprobablyallusedto.
Now, before I gostartbreakingthisdownandimplementingthecommandpatternforthiscalculator, I wanttotalk a littlebitaboutwhatthecommandpatternisandwhyyouwouldevenuseit.
Andtheideaofthecommandpatternistotakethedifferentoperationsthatyouwantsomethingtodoandencapsulatethemintoindividualcommandsthathave a performandthenundomethod.
We'rejustgonnacreditclassfor I wanttocallitheretheAdCommandandthisAdCommandisgoingtotake a constructorandhisconstructorsjustgoingtotaketheamountwewanttoaddsoexcitedvaluetoadd.
It's essentiallythesamethingasthevaluewepassedtothefunctionwerealreadycalling, theninsidehere, as I mentioned, weneedtohaveanexecutefunctionandthenweneedtohaveanunduefunction.
Becausethiscommandisnolongerinthecalculator, itcanbeusedforthingsthataren't thecalculatorwecoulddoadded, forexample, in a gamewecouldhavesomedamagecalculatorthatdoes.
Adding, Whilethiscouldbe a calculatoron a desktopof a computer, theycouldbothstillusethisadcommand.
Butwhenyoucanthinkofthingssuch a savingusersto a database, that's somethingthathavinganunduefunctionforisincrediblyusefulbecausetheundueisreallycomplex.
Justworkseamlesslycommands, ideallystartoutreallysmallandthenbuilduponthemselvestomakemorecomplexcommands, whichthenbuilttomakeevenmorecomplexcommandsandsoonuntilyouhaveonesinglecommandthatdoeseverythingyouneededtodofor a specificinstance.
Take, forexample, a button.
Youknow, whenyouclosemostapplications a lotoftimesyou'regonnagetthreeoptions.
We'llcreate a classcalledad, thenmultiplycommandandinsideofthathere, we'regonnado a constructor, andit's gonnatakeourvaluetoeaddaswellasourvaluetomultiply.