If you want to make your application maintainable and scalable, following right coding practices is very important. In order to write such a code- using well tested, proven and reusable software solutions can come quite handy. And that is what design patterns are all about-
1. they are proven and well-tested solutions for frequently occurring software development problems.
2. they make your code scalable by structuring your code-base the correct way.
3. they are reusable for similar occurring problems.
Imagine a service, where all the shared states and behaviors are maintained. A service that is shared throughout the application. In this case, creating multiple instances of such a service will defeat the whole purpose of having something shared across the application.
Therefore, what we would like to have is a single instance of such a service injected wherever necessary in the application.
A great analogy could be a coffee machine in your department. You don’t bring your own coffee machine to the office. A single coffee machine is shared amongst all the colleagues.
But, how do we do it?
Well, this is where Singleton design pattern comes to our rescue. This pattern gives us the ability to instantiate and return only one object, irrespective of repeated calls to its constructor. Once the singleton object is created, it returns itself every time some client tries to instantiate the class.
Above is a very simple implementation of Singleton where access to “coffeeMachineInstance” object is given via “getInstance” function. Every time a client wants to instantiate “CoffeeMachine” it will call its “getInstance” function which will return the same “coffeeMachineInstance” object. The class “CoffeeMachine” will create the object if it is not already instantiated.
This can be proved using below code snippet. You can see that “machine1” and “machine2” refers to the same object.
Details on IIFE and its benefits deserves its own separate post- which will be covered in future. But simply put, they are functions which executes themselves immediately after declaring. They are used to create a “private” scope by creating a closure.
If you are not familiar with closure, you might want to know what closures are in simplest terms, before reading further.
IIFE looks something like this:
So, using IIFE we can create modules like below, which can then be used in other places in our application.
In the above example, a module named “MyModule” is created which can then be imported in other modules and used to access the private members of its closure via public methods- “publicFunction” in this case. But, when you try to directly access the private members like “privateVariable”- they are undefined.
Above is a simple explanation on two very popular design patterns. So now, you should be able to recognize them and use them in your next project- which I highly recommend you to do so.