SlideShare a Scribd company logo
JavaScript Design
Patterns
By D Balaji
Note
• PPT prepared for educational purpose
• There may be errors, use your own discretion wherever reqd
https://www.linkedin.com/in/dhbalaji
2
Agenda
• Background & Prerequisites
• Discussion
• Applications
• Advanced
• Take home activity
https://www.linkedin.com/in/dhbalaji
3
Background
• Design patterns are reusable solutions to commonly occurring
problems.
• They provide common vocabulary to describe solutions.
• Not every solution, algorithm can qualify to be called a pattern.
• Prerequisites
• Proficiency in JavaScript concepts
• Want to write beautiful code
https://www.linkedin.com/in/dhbalaji
4
Lets discuss
• Pattern has 2 parts to it
• Process
• Output
• Rule of three.
• Fitness of purpose
• Usefulness
• Applicability
• Anti-patterns – Bad solution to a problem. For example modifying
Object Prototype methods is a anti pattern
https://www.linkedin.com/in/dhbalaji
5
Types of design patterns
• Creational design patterns
• Constructor
• Factory
• Abstract
• Prototype
• Singleton
• Builder
• Structural design patterns
• Decorator
• Façade
• Adaptor
• Proxy
• Flyweight
https://www.linkedin.com/in/dhbalaji
6
Types of patterns contd.
• Behavioural design pattern
• Iterator
• Mediator
• Observer
• Visitor
https://www.linkedin.com/in/dhbalaji
7
JS – Creational patterns
• It deals with how we create new objects
• var a = {}
• Other alternatives
• Object.assign
• Object.create
• Object.defineProperties
https://www.linkedin.com/in/dhbalaji
8
JS – Constructor pattern
• Constructors are used to create a specific type of objects
• When you call a Constructor to create a method in JS, the
properties of constructor prototype are copied to new Object
function Car(name) {
this.name = name;
}
var newCar = new Car(‘tata’);
https://www.linkedin.com/in/dhbalaji
9
Singleton pattern
• Create a object if no instance found, else reference the existing
instance
• Only one instance per app
• Example: namespaces
https://www.linkedin.com/in/dhbalaji
10
Module pattern
• Helps the code in keeping it organized
• Examples:
• Object literals – tie together properties and values
• Can provide public and private variables
• IIFE is also a module pattern
https://www.linkedin.com/in/dhbalaji
11
Revealing module pattern
• In module pattern all the properties and methods were exposed
• In Revealing module, the developer chooses to make few
properties and methods available
https://www.linkedin.com/in/dhbalaji
12
Observer pattern
• Also known as publish/subscribe pattern
• Watch for other object to change and perform an action
• We establish listen and broadcast relationship
• Client -> subscribes and unsubscribe for watching changes
• Producer notifies the client
• Promotes loose coupling
• One of the best tools out there.
https://www.linkedin.com/in/dhbalaji
13
Mediator pattern
• Mediator in real life assists in negotiation and conflict resolution
• Mediator pattern provides a uniform interface to communicate
• In pub/sub the object passed around did not have clear agreement
or contract. In mediator problem, this problem is solved
https://www.linkedin.com/in/dhbalaji
14
Prototype pattern
• Creation of a new object based on a template of an existing
object through cloning
• Used in prototypal inheritance
https://www.linkedin.com/in/dhbalaji
15
Command pattern
• Encapsulate method invocation, requests into a single object
• Separate the code to execute a method with or without a
parameter
• Classes often have methods like run, execute
https://www.linkedin.com/in/dhbalaji
16
Facade pattern
• Façade gives a different version of reality, relates to a pleasing
exterior of a building in construction domain.
• We provide a high level interface for large code
• Provides ease of use and smaller code to use
https://www.linkedin.com/in/dhbalaji
17
Factory pattern
• Helps in creating objects
• Developer need not be worried about the class
• Used when the steps to create a new object is complex
• Abstract factory
• Uses a combination of factories to create new objects
https://www.linkedin.com/in/dhbalaji
18
Mixin pattern
• Classes which provide functionality to be inherited by other class
• JS does not support multiple inheritance
https://www.linkedin.com/in/dhbalaji
19
Decorator pattern
• Introduces new functionality to the code without modifying the
code underneath
• Variants
• Pseudo classical decorator
• Uses interfaces
• Interface is a way of telling the must have properties and methods
in a class
https://www.linkedin.com/in/dhbalaji
20
Flyweight
• Useful solution for slow, non performant code
• It’s an approach where common non performant code is taken and
kept in a shared location or single object
• Example: Google maps integration
https://www.linkedin.com/in/dhbalaji
21
MV* pattern
• Improves application organization through separation of concerns
• Easy maintanence
• Domain element or date  Model
• View and Presentation  View
• Handling user interaction and event  Controller
• MVVM  Data binding between view and the model
https://www.linkedin.com/in/dhbalaji
22
Name spacing patterns
• Namespacing is a technique to avoid collision with objects in the
global namespace
• Useful in organizing blocks of functionality
• JavaScript has no built in support for namespaces
• We use closures and objects to achieve namespaces
https://www.linkedin.com/in/dhbalaji
23
Types of namespaces
• Single global variables
• Prefix namespacing
• Nested namespacing
• IIFE
• Namespace injection
https://www.linkedin.com/in/dhbalaji
24
Lazy initialization
• Allows us to delay expensive process like creating objects etc until
they are needed
• Lazy loading is used when we want to load additional page
resources when needed
https://www.linkedin.com/in/dhbalaji
25
Adapter pattern
• Translates the interface of a class into an interface compatible
with the current system.
https://www.linkedin.com/in/dhbalaji
26
Façade pattern
• Provides a simpler interface to larger body of code.
https://www.linkedin.com/in/dhbalaji
27
Iterator pattern
• Allows us to access the elements of the object without needing to
expose its form.
• We can also iterate a string in JavaScript and read its value
https://www.linkedin.com/in/dhbalaji
28
Strategy pattern
• The algorithm to be deployed is decided at runtime.
• The algorithms used are subjective to the client or datatype or
changed environment.
https://www.linkedin.com/in/dhbalaji
29
Proxy pattern
• A class which serves as a interface for something else.
• You call one method in a class which in turns calls something else
https://www.linkedin.com/in/dhbalaji
30
Builder pattern
• Similar to chaining methods
• The output of a step is used as input to an another and the details
are abstracted to the user.
https://www.linkedin.com/in/dhbalaji
31
Other terms
• Loose coupling – easy maintainability by removing dependencies
wherever possible
• AMD – asynchronous modules which are loaded whenever required
with help of script loaded like requires
• Plugin is a software program that adds a missing functionality in a
program
https://www.linkedin.com/in/dhbalaji
32
Exercise
• Identify pros & cons of every pattern
• Take any library, identify the patterns used. For this you have to
look at source code. Go with jquery
• Prototypal vs Class based inheritance
https://www.linkedin.com/in/dhbalaji
33

More Related Content

PPTX
Link your Way to Successful Content Management with MadCap Flare
PPTX
Generic Repository Pattern with ASP.NET MVC and EF
PPSX
Oop principles
PDF
Javascript Design Patterns
PPT
Design pattern
PPTX
Design pattern of software words computer .pptx
PPTX
Design patterns
PPTX
JavaScripters Event Sep 17, 2016 · 2:00 PM: Scalable Javascript Design Patterns
Link your Way to Successful Content Management with MadCap Flare
Generic Repository Pattern with ASP.NET MVC and EF
Oop principles
Javascript Design Patterns
Design pattern
Design pattern of software words computer .pptx
Design patterns
JavaScripters Event Sep 17, 2016 · 2:00 PM: Scalable Javascript Design Patterns

Similar to JavaScript design patterns introduction (20)

PPT
Design_Patterns_Dr.CM.ppt
PDF
Design patterns in javascript
PPTX
Javascript Design Patterns
PDF
software engineering Design Patterns.pdf
PPTX
Nodejs Chapter 3 - Design Pattern
PDF
Mini-Training: Javascript Patterns
PDF
Modern JavaScript Applications: Design Patterns
PPTX
Sofwear deasign and need of design pattern
PDF
Unleashing the Power of Modern Javascript Development
PDF
Design Pattern in Software Engineering
PDF
Javascript Design Patterns
PDF
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
PDF
Design patterns for beginners (1/ 2)
PPTX
Javascript from beginning to modern
PDF
Design Patterns - GOF
PPSX
Design patterns
PPTX
Design patterns software re engineering lect 10
PDF
Design patterns
PDF
Mastering Design Patterns in Java: A Comprehensive Guide
PDF
Design patterns in java script, jquery, angularjs
Design_Patterns_Dr.CM.ppt
Design patterns in javascript
Javascript Design Patterns
software engineering Design Patterns.pdf
Nodejs Chapter 3 - Design Pattern
Mini-Training: Javascript Patterns
Modern JavaScript Applications: Design Patterns
Sofwear deasign and need of design pattern
Unleashing the Power of Modern Javascript Development
Design Pattern in Software Engineering
Javascript Design Patterns
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Design patterns for beginners (1/ 2)
Javascript from beginning to modern
Design Patterns - GOF
Design patterns
Design patterns software re engineering lect 10
Design patterns
Mastering Design Patterns in Java: A Comprehensive Guide
Design patterns in java script, jquery, angularjs
Ad

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Cloud computing and distributed systems.
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Electronic commerce courselecture one. Pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
cuic standard and advanced reporting.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
20250228 LYD VKU AI Blended-Learning.pptx
madgavkar20181017ppt McKinsey Presentation.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Understanding_Digital_Forensics_Presentation.pptx
Cloud computing and distributed systems.
Sensors and Actuators in IoT Systems using pdf
Network Security Unit 5.pdf for BCA BBA.
Electronic commerce courselecture one. Pdf
MYSQL Presentation for SQL database connectivity
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced Soft Computing BINUS July 2025.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Monthly Chronicles - July 2025
Diabetes mellitus diagnosis method based random forest with bat algorithm
cuic standard and advanced reporting.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Ad

JavaScript design patterns introduction

  • 2. Note • PPT prepared for educational purpose • There may be errors, use your own discretion wherever reqd https://www.linkedin.com/in/dhbalaji 2
  • 3. Agenda • Background & Prerequisites • Discussion • Applications • Advanced • Take home activity https://www.linkedin.com/in/dhbalaji 3
  • 4. Background • Design patterns are reusable solutions to commonly occurring problems. • They provide common vocabulary to describe solutions. • Not every solution, algorithm can qualify to be called a pattern. • Prerequisites • Proficiency in JavaScript concepts • Want to write beautiful code https://www.linkedin.com/in/dhbalaji 4
  • 5. Lets discuss • Pattern has 2 parts to it • Process • Output • Rule of three. • Fitness of purpose • Usefulness • Applicability • Anti-patterns – Bad solution to a problem. For example modifying Object Prototype methods is a anti pattern https://www.linkedin.com/in/dhbalaji 5
  • 6. Types of design patterns • Creational design patterns • Constructor • Factory • Abstract • Prototype • Singleton • Builder • Structural design patterns • Decorator • Façade • Adaptor • Proxy • Flyweight https://www.linkedin.com/in/dhbalaji 6
  • 7. Types of patterns contd. • Behavioural design pattern • Iterator • Mediator • Observer • Visitor https://www.linkedin.com/in/dhbalaji 7
  • 8. JS – Creational patterns • It deals with how we create new objects • var a = {} • Other alternatives • Object.assign • Object.create • Object.defineProperties https://www.linkedin.com/in/dhbalaji 8
  • 9. JS – Constructor pattern • Constructors are used to create a specific type of objects • When you call a Constructor to create a method in JS, the properties of constructor prototype are copied to new Object function Car(name) { this.name = name; } var newCar = new Car(‘tata’); https://www.linkedin.com/in/dhbalaji 9
  • 10. Singleton pattern • Create a object if no instance found, else reference the existing instance • Only one instance per app • Example: namespaces https://www.linkedin.com/in/dhbalaji 10
  • 11. Module pattern • Helps the code in keeping it organized • Examples: • Object literals – tie together properties and values • Can provide public and private variables • IIFE is also a module pattern https://www.linkedin.com/in/dhbalaji 11
  • 12. Revealing module pattern • In module pattern all the properties and methods were exposed • In Revealing module, the developer chooses to make few properties and methods available https://www.linkedin.com/in/dhbalaji 12
  • 13. Observer pattern • Also known as publish/subscribe pattern • Watch for other object to change and perform an action • We establish listen and broadcast relationship • Client -> subscribes and unsubscribe for watching changes • Producer notifies the client • Promotes loose coupling • One of the best tools out there. https://www.linkedin.com/in/dhbalaji 13
  • 14. Mediator pattern • Mediator in real life assists in negotiation and conflict resolution • Mediator pattern provides a uniform interface to communicate • In pub/sub the object passed around did not have clear agreement or contract. In mediator problem, this problem is solved https://www.linkedin.com/in/dhbalaji 14
  • 15. Prototype pattern • Creation of a new object based on a template of an existing object through cloning • Used in prototypal inheritance https://www.linkedin.com/in/dhbalaji 15
  • 16. Command pattern • Encapsulate method invocation, requests into a single object • Separate the code to execute a method with or without a parameter • Classes often have methods like run, execute https://www.linkedin.com/in/dhbalaji 16
  • 17. Facade pattern • Façade gives a different version of reality, relates to a pleasing exterior of a building in construction domain. • We provide a high level interface for large code • Provides ease of use and smaller code to use https://www.linkedin.com/in/dhbalaji 17
  • 18. Factory pattern • Helps in creating objects • Developer need not be worried about the class • Used when the steps to create a new object is complex • Abstract factory • Uses a combination of factories to create new objects https://www.linkedin.com/in/dhbalaji 18
  • 19. Mixin pattern • Classes which provide functionality to be inherited by other class • JS does not support multiple inheritance https://www.linkedin.com/in/dhbalaji 19
  • 20. Decorator pattern • Introduces new functionality to the code without modifying the code underneath • Variants • Pseudo classical decorator • Uses interfaces • Interface is a way of telling the must have properties and methods in a class https://www.linkedin.com/in/dhbalaji 20
  • 21. Flyweight • Useful solution for slow, non performant code • It’s an approach where common non performant code is taken and kept in a shared location or single object • Example: Google maps integration https://www.linkedin.com/in/dhbalaji 21
  • 22. MV* pattern • Improves application organization through separation of concerns • Easy maintanence • Domain element or date  Model • View and Presentation  View • Handling user interaction and event  Controller • MVVM  Data binding between view and the model https://www.linkedin.com/in/dhbalaji 22
  • 23. Name spacing patterns • Namespacing is a technique to avoid collision with objects in the global namespace • Useful in organizing blocks of functionality • JavaScript has no built in support for namespaces • We use closures and objects to achieve namespaces https://www.linkedin.com/in/dhbalaji 23
  • 24. Types of namespaces • Single global variables • Prefix namespacing • Nested namespacing • IIFE • Namespace injection https://www.linkedin.com/in/dhbalaji 24
  • 25. Lazy initialization • Allows us to delay expensive process like creating objects etc until they are needed • Lazy loading is used when we want to load additional page resources when needed https://www.linkedin.com/in/dhbalaji 25
  • 26. Adapter pattern • Translates the interface of a class into an interface compatible with the current system. https://www.linkedin.com/in/dhbalaji 26
  • 27. Façade pattern • Provides a simpler interface to larger body of code. https://www.linkedin.com/in/dhbalaji 27
  • 28. Iterator pattern • Allows us to access the elements of the object without needing to expose its form. • We can also iterate a string in JavaScript and read its value https://www.linkedin.com/in/dhbalaji 28
  • 29. Strategy pattern • The algorithm to be deployed is decided at runtime. • The algorithms used are subjective to the client or datatype or changed environment. https://www.linkedin.com/in/dhbalaji 29
  • 30. Proxy pattern • A class which serves as a interface for something else. • You call one method in a class which in turns calls something else https://www.linkedin.com/in/dhbalaji 30
  • 31. Builder pattern • Similar to chaining methods • The output of a step is used as input to an another and the details are abstracted to the user. https://www.linkedin.com/in/dhbalaji 31
  • 32. Other terms • Loose coupling – easy maintainability by removing dependencies wherever possible • AMD – asynchronous modules which are loaded whenever required with help of script loaded like requires • Plugin is a software program that adds a missing functionality in a program https://www.linkedin.com/in/dhbalaji 32
  • 33. Exercise • Identify pros & cons of every pattern • Take any library, identify the patterns used. For this you have to look at source code. Go with jquery • Prototypal vs Class based inheritance https://www.linkedin.com/in/dhbalaji 33