Design Patterns in JavaScript — Part 1
If you have worked on any JavaScript project, believe me you must have used at least one design pattern. You can consider design pattern as code pattern. Choosing the right design pattern helps us to write efficient and organized code.
Design patterns are broadly categorized into following:
- Creational
- Structural
- Behavioral
Creational design patterns are used for creating objects. In this blog we will be covering the mostly used creational design patterns. We will be covering structural and behavioral design patterns in upcoming blogs.
1. Constructor Design Pattern
- This is the simplest creational design pattern.
- Whenever we use
new
keyword, we will be using this design pattern. - This design pattern is used to create new objects using
new
keyword.

- Code of Constructor design pattern in JavaScript will be like below:
2. Prototype Design Pattern
- This is a design pattern using which properties and methods are added/updated in prototype of objects.
- Code of Prototype design pattern in JavaScript will be like below:
3. Module Design Pattern
- This design pattern provides encapsulation.
- It was developed in 2003.
- Code of Module design pattern in JavaScript will be like below:
4. Enhanced Module Design Pattern
- It is an enhanced version of Module Pattern.
- It provides all features of module pattern but with cleaner coding style.
- It is highly used in third party libraries.
- Code of Enhanced Module design pattern in JavaScript will be like below:
5. Singleton Design Pattern
- This design pattern makes sure that only one Instance is created throughout the application
- Example: Task Manager of Windows

- Code of Singleton design pattern in JavaScript will be like below:
6. Factory
- This design pattern simplifies object creation.
- It creates different types of objects based on requirement.
- Code of Factory design pattern in JavaScript will be like below:
Conclusion
Design patterns are very useful for creating efficient applications. Each design pattern has some unique feature which helps us to write better and organized code.