JavaScript Design Patterns

Nairi Harutyunyan / nairihar

Node.js Armenia Community

Backend Engineer / Screenful

#nodejsarmenia 💚

t.me/nodejsarmenia

About me

Why Design Patterns?

What is Design Pattern?

*Design Patterns

*Singleton

*Factory

*Middleware

*Command

*Observer

*Module

*Adapter

*Strategy

*Pub/Sub

*Constructor

Singleton

Restricts the instantiation of a class to one single* instance

Factory

Creates objects without specifying the exact class

Module

Similar to a singleton, it has only one instance and exposes its members, but it doesn’t have any kind of internal state

Constructor

Allows to create multiple instances of the same script or plugin 

Adapter

Allows us to us access the functionality of an object using a different interface

Adapter

Adaptee

  methodA()
  methodB()
  methodD()
  methodC()

Strategy

Encapsulates an algorithm inside a class/function separating the selection from the implementation

Observer

A way of notifying change to a number of Subjects

Pub/Sub

A way of publishing notification to a number of subscribers

Observer

Subscriber

Publisher

Broker

Subscriber

subscribe

notify

Command

Encapsulate a command request as an object to enable, logging and/or queuing of requests, and provides error-handling for unhandled requests

Middleware

Functions that have access to the request object {req}, the response object {res}, and the (next) middleware function

Middleware 1

  request {}
  response {}
  next ()

Middleware 2

  request {}
  response {}
  next ()

Middleware3

  request {}
  response {}
  next ()
Error ...
response object
response object

Thank You!

nairihar

#jsconfam19

See you all at Node.js conf 2020 :)

Made with Slides.com