课程: JavaScript: Five Advanced Challenges and Concepts
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Building robust applications - JavaScript教程
课程: JavaScript: Five Advanced Challenges and Concepts
Building robust applications
- Sometimes you need to create a class, only one instance can be created from, for example, if you have a logger class to log anytime a certain event happens, you don't want to have numerous instances of that same logger. Another example would be configuration settings. Only one instance of a class should do the configuration, have several configurators and things get messy real quick. To this end, we can use the Singleton Pattern. In short, a Singleton ensures only one instance of a class can be created, and if you try to create a new instance, when one already exists, the existing one is automatically returned. In addition to Singleton classes, you can further protect the original class or intercept any interaction with the class or object using a proxy object. A proxy object sits between your function call and the actual object or class you're calling and can intercept and perform actions on the request before it's passed to…