Module Design Pattern in JavaScript

Bhuwan Pandey
2 min readSep 23, 2020

--

There is a diverse list of design patterns in Javascript among which few are important to know and widely used.

  • Module Design Pattern
  • Revealing Module Design Pattern
  • Prototype Design Pattern
  • Observer Design Pattern
  • Singleton Design Pattern

Important thing to understand here is before jumping into design patterns one should have a clear understanding of below points.

- Why we really care about design patterns?

- What is the problem we are trying to solve using design patterns?

- In which context design patterns should be used?

If you already have answers to above questions then continue reading

Module Design Pattern

  • Modules are an integral piece of any robust application’s architecture and typically help in keeping the units of code for a project both cleanly separated and organized.
  • Piece of encapsulated code which has its own defined scope as well as access to global scope.
  • Helps to achieve Encapsulation as well as abstraction with private members and publicly exposed members.
  • Helps in shielding particular parts from the global scope.
Sample JavaScript Module

“One of the use case you could think of modular design patterns is shopping kart app where adding/removing items from cart, add items to wish list can be public methods and any other actions like calculating price and any other internal calculations can be private to module”

Another variation of module design pattern is revealing design pattern.

As the name suggests the Revealing Module Pattern uses the return statement to reveal publicly accessible methods, we can return a object literal that ‘reveals’ only the methods or properties we want to be publicly available.

Revealing Module in JavaScript

Advantages of Modular and Revealing Module Design Pattern

  • Clean code approach
  • Less pollution in the global namespace
  • Functions and variable localization through closures
  • Explicitly defined public methods and variables which lead to increased readability.

--

--

Bhuwan Pandey
Bhuwan Pandey

Written by Bhuwan Pandey

0 Followers

JavaScript Developer.

No responses yet