NodeJS : Design Patterns
27 Feb 2021
Acknowledgements
• Organising team
• O.P. Pachoriya & Sanny Kumar from Talentica.
2
Agenda
• What are design patterns?
• Creational design patterns
• Singleton Pattern
• Prototype Pattern
• Factory Method Pattern
• Builder Pattern
3
Agenda contd.
• Behavioural design patterns
• The chain of responsibility pattern
• The command pattern
• The iterator pattern
• The observer pattern
• The strategy pattern
4
What should you know
• Understanding of JavaScript
• Some working knowledge with Node.js
• Experience working in Object Oriented
Programming environment
5
Procedural Programming
6
f() x
x
f()
Program
f()
f()
f()
f()
f()
f()
f()
f()
OOP (A brush up)
7
f() x
f() x
Properties
Methods
Object
8
OOP Benefits
9
• Encapsulation: Helps reduce complexity & promotes reusability
• Abstraction: Reduces complexity & isolates impact of changes
• Inheritance: Eliminates redundant code
• Polymorphism: Promotes refactoring of long if/else, switch/case
statements
What are design patterns?
Reusable, reliable solutions to problem that we face
every day in software development
10
Why Design Patterns?
• Cataloged solutions
• Reusable in many different situations
• Well documented
• Language for collaboration
• Improve architecture
• Write better program
• Become a better programmer
11
Gang of Four (GoF)
• Elements of Reusable Object-Oriented Software
- Gang of Four, 1994
• Design Patterns definition
• Pattern name
• The problem
• The solution
• Consequences
12
Classical design patterns
13
Creational
These patterns have to do with
class instantiation, the creation of
object instances in our applications
Structural
Structural design patterns have to
do with the way objects are
composed or put together
Behavioral
Behavioral design patterns define
how objects interact with one
another
Other patterns
• Module
• Callback
• Middleware
• Reactor
• Blockchain
• Scheduler
• Publisher Subscriber
14
Singleton Pattern
Ensure a class only has one instance and provide a
global point of access to it
15
Demo - Singleton
• Singleton Problem
• Class instantiates multiple objects
• Singleton Pattern solution
• Create single instance of the class throughout execution
• Singletons in node.js
• Module caching mechanism
16
Prototype Pattern
Specify the kinds of objects to create using
prototypical instance and create new objects by
copying this prototype
17
Demo - Prototype
• Prototype Problem
• Object creation is repetitive and costly
• Prototype Pattern solution
• The Clone() method
18
Factory Method Pattern
Define an interface for creating an object, but let
subclasses decide which class to instantiate. Factory
method lets a class defer instantiation to subclasses
19
Demo - Factory Method
• Factory Method problem
• Object creation with exposed logic
• Factory Method pattern solution
• Provide common interface to create new objects
20
Builder pattern
Separate the construction of a complex object from
its representation so that the same construction
process can create different representations
21
Demo - Builder
• Builder Problem
• Telescoping Constructor Anti Pattern
• Builder pattern solution
• Reduce parameters to the properties, make highly
readable method calls
22
Chain of responsibility
The GoF defines the intent of the Chain of Responsibility pattern as, to avoid
coupling the sender of a request and its receiver by giving more than one object a
chance to handle the request. Chain the receiving objects and pass the request
along the chain until an object handles it.
23
Examples?
• People
• Assembly line
• Customer service calls
• Programming
• Express middleware
24
25
27
28
29
The command pattern
The Gang of Four defines the intent the command pattern as encapsulating a
request as an object, thereby letting you parameterize clients with different
requests, queue or log requests, and support undoable operations
30
Examples?
• People
• A customer in a restaurant
• Programming
• An editor application
31
32
33
34
35
The iterator pattern
The Gang of Four defines the intent of the iterator as providing a way to access
the elements of an aggregate object (Stack, trees etc.,) sequentially without
exposing its underlying representation.
36
Examples?
• People
• A tourist without/with guides
• Programming
• Traversing in different ways on a given collection
37
38
39
40
The Observer pattern
The Gang of Four defines the intent of the observer pattern as, "Defining a one-
to-many dependency between objects so that when one object changes state, all
of its dependents are notified and updated automatically."
41
Examples?
• People
• Newsletters
• Programming
• Event based triggers, On successful transaction,
generate receipt, send payment acknowlegement email,
send receipt etc.
42
43
44
45
46
The strategy pattern
The Gang of Four defines a strategy as a family of algorithms, encapsulate each
one, and make them interchangeable. Strategy lets the algorithm vary
independently from the clients that use it.
47
Examples?
• People
• Travelling to a destination
• Programming
• Support for different payment services
• Support for different logging services
48
49
50
51
Design patterns are cool!
52
Let's learn more
about them.
Let's use them. And let's introduce
the new ones!
Topics for later talks
• Structural design patterns
• Adapter patterns
• Proxy patterns
• Composite patterns
• Decorator patterns
• Other design patterns
53
Resources
• A course by Alex Banks on LinkedIn Learning
• A course by Sachin Bhatnagar on Udemy
• https://refactoring.guru/design-patterns
• Different resourses by Mosh Hamedani
54
Thank You
55

Nodejs Chapter 3 - Design Pattern

  • 1.
    NodeJS : DesignPatterns 27 Feb 2021
  • 2.
    Acknowledgements • Organising team •O.P. Pachoriya & Sanny Kumar from Talentica. 2
  • 3.
    Agenda • What aredesign patterns? • Creational design patterns • Singleton Pattern • Prototype Pattern • Factory Method Pattern • Builder Pattern 3
  • 4.
    Agenda contd. • Behaviouraldesign patterns • The chain of responsibility pattern • The command pattern • The iterator pattern • The observer pattern • The strategy pattern 4
  • 5.
    What should youknow • Understanding of JavaScript • Some working knowledge with Node.js • Experience working in Object Oriented Programming environment 5
  • 6.
  • 7.
    OOP (A brushup) 7 f() x f() x Properties Methods Object
  • 8.
  • 9.
    OOP Benefits 9 • Encapsulation:Helps reduce complexity & promotes reusability • Abstraction: Reduces complexity & isolates impact of changes • Inheritance: Eliminates redundant code • Polymorphism: Promotes refactoring of long if/else, switch/case statements
  • 10.
    What are designpatterns? Reusable, reliable solutions to problem that we face every day in software development 10
  • 11.
    Why Design Patterns? •Cataloged solutions • Reusable in many different situations • Well documented • Language for collaboration • Improve architecture • Write better program • Become a better programmer 11
  • 12.
    Gang of Four(GoF) • Elements of Reusable Object-Oriented Software - Gang of Four, 1994 • Design Patterns definition • Pattern name • The problem • The solution • Consequences 12
  • 13.
    Classical design patterns 13 Creational Thesepatterns have to do with class instantiation, the creation of object instances in our applications Structural Structural design patterns have to do with the way objects are composed or put together Behavioral Behavioral design patterns define how objects interact with one another
  • 14.
    Other patterns • Module •Callback • Middleware • Reactor • Blockchain • Scheduler • Publisher Subscriber 14
  • 15.
    Singleton Pattern Ensure aclass only has one instance and provide a global point of access to it 15
  • 16.
    Demo - Singleton •Singleton Problem • Class instantiates multiple objects • Singleton Pattern solution • Create single instance of the class throughout execution • Singletons in node.js • Module caching mechanism 16
  • 17.
    Prototype Pattern Specify thekinds of objects to create using prototypical instance and create new objects by copying this prototype 17
  • 18.
    Demo - Prototype •Prototype Problem • Object creation is repetitive and costly • Prototype Pattern solution • The Clone() method 18
  • 19.
    Factory Method Pattern Definean interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses 19
  • 20.
    Demo - FactoryMethod • Factory Method problem • Object creation with exposed logic • Factory Method pattern solution • Provide common interface to create new objects 20
  • 21.
    Builder pattern Separate theconstruction of a complex object from its representation so that the same construction process can create different representations 21
  • 22.
    Demo - Builder •Builder Problem • Telescoping Constructor Anti Pattern • Builder pattern solution • Reduce parameters to the properties, make highly readable method calls 22
  • 23.
    Chain of responsibility TheGoF defines the intent of the Chain of Responsibility pattern as, to avoid coupling the sender of a request and its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. 23
  • 24.
    Examples? • People • Assemblyline • Customer service calls • Programming • Express middleware 24
  • 25.
  • 27.
  • 28.
  • 29.
  • 30.
    The command pattern TheGang of Four defines the intent the command pattern as encapsulating a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations 30
  • 31.
    Examples? • People • Acustomer in a restaurant • Programming • An editor application 31
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
    The iterator pattern TheGang of Four defines the intent of the iterator as providing a way to access the elements of an aggregate object (Stack, trees etc.,) sequentially without exposing its underlying representation. 36
  • 37.
    Examples? • People • Atourist without/with guides • Programming • Traversing in different ways on a given collection 37
  • 38.
  • 39.
  • 40.
  • 41.
    The Observer pattern TheGang of Four defines the intent of the observer pattern as, "Defining a one- to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically." 41
  • 42.
    Examples? • People • Newsletters •Programming • Event based triggers, On successful transaction, generate receipt, send payment acknowlegement email, send receipt etc. 42
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
    The strategy pattern TheGang of Four defines a strategy as a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it. 47
  • 48.
    Examples? • People • Travellingto a destination • Programming • Support for different payment services • Support for different logging services 48
  • 49.
  • 50.
  • 51.
  • 52.
    Design patterns arecool! 52 Let's learn more about them. Let's use them. And let's introduce the new ones!
  • 53.
    Topics for latertalks • Structural design patterns • Adapter patterns • Proxy patterns • Composite patterns • Decorator patterns • Other design patterns 53
  • 54.
    Resources • A courseby Alex Banks on LinkedIn Learning • A course by Sachin Bhatnagar on Udemy • https://refactoring.guru/design-patterns • Different resourses by Mosh Hamedani 54
  • 55.