Creational patterns
Creational patterns are used for object instantiation. They are used in situations where the basic form of object creation could result in design problems or increase complexity to the design. In the following section, we will discuss all four creational patterns mentioned previously, and how to implement them in JavaScript.
Singleton design pattern
Singleton is the most widely used pattern. It is used in scenarios where we need to share the same instance of a class or function (in terms of JavaScript) between different objects. It ensures that there is only one instance of particular object which can be accessed globally at any point:
In a singleton pattern, the constructor should be private which restricts the user to create objects using a new
keyword and exposes one method that creates an instance and verifies that only one instance exists. A simple example could be a logger object that writes the log to the browser's console window:
<script> var Logger = (function...