Why should we follow a specific designpattern?- It helps to create modular and functional code- Unit testable/reusable code since entire programmingparadigm shift to client side
1. Prototype PatternThe Prototype Pattern can be broken out into two mainsections including a constructor section and a prototypesection. Prototyping allows functions and properties to beassociated with objects. However, instead of each objectinstance getting a copy of all functions/properties each timean object is created, only one set of functions/propertiesexists across all objects resulting in less memoryconsumption. In other words, functions and properties aredefined once per prototype rather than once per object.Lets see how it works.
How does it work?As you can see, there is no separateconstructor section and method definitionsection like we have seen in Prototypepattern.Like mentioned previously, calculator objectis initialized through self execution of themethod body. That is, an anonymousfunction is executed with eq as parameter,and the result of the function (in this case,object literal after the return statement) isstored in the calculator object.Very important thing to note about ModulePattern is that, Normally each time a newobject is created a new set of functions willbe created. In this case 5 objects will create5 x 4 = 20 method definitions!. So its notfriendly on memory usage. So, that is thereason this pattern has a self executingmethod definition like given in the examplewhich in turn implements the Singletonconcept.
3. Revealing Module PatternModule Pattern and Revealing Module Pattern are conceptually same,but differs in the way member functions are defined.
How does it work?As you can see, how methods are defineddiffers from Module Pattern. Methoddefinitions are given names through var andthe these variables are used in the returnstatement rather than function definitions likewe have seen in Module Pattern. Oneadvantage of using Revealing ModulePattern over Module Pattern is that we canhave private methods. In this example addmethod can be removed from the objectliteral in the return statement so that theobject will expose only subtract, multiply anddivide methods. But these 3 methods canaccess add method.
What are the other benefits?We have done the POC for patientfinancial.js and it workslike charm.Let’s all follow this pattern for better eco system.What next?Code will be unit testable(framework like jasmine etc can beadoptable)Code will be reusableClean html and js separation of concern