Design Patterns in JavaScript — Part 1

Yuvraj Patil
2 min readSep 17, 2021

--

Photo by Nick Fewings on Unsplash

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.
Design Pattern - Constructor
  • Code of Constructor design pattern in JavaScript will be like below:
Constructor Design Pattern Example by Yuvraj Patil

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:
Prototype Design Pattern Example by Yuvraj Patil

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:
Module Design Pattern Example by Yuvraj Patil

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:
Enhanced Module Design Pattern Example by Yuvraj Patil

5. Singleton Design Pattern

  • This design pattern makes sure that only one Instance is created throughout the application
  • Example: Task Manager of Windows
Design Pattern - Singleton
  • Code of Singleton design pattern in JavaScript will be like below:
Singletone Design Pattern Example by Yuvraj Patil

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:
Factory Design Pattern Example by Yuvraj Patil

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Yuvraj Patil
Yuvraj Patil

Written by Yuvraj Patil

Riding the dragon in realm of React Native. Website: https://.www.yuvrajpatil.com

Responses (1)

Write a response