Design patterns in Javascript

Arun Rajeevan
3 min readMar 5, 2019

Object Creation Patterns:

1. Abstract factory

What is it?
In object-oriented programming a Factory is an object that creates other objects. An Abstract Factory has abstracted out a theme which is shared by the newly created objects.

When you need this pattern?
Scenarios in which the creation process involves object caching, sharing or re-using of objects, complex logic, or applications that maintain object and type counts, and objects that interact with different resources or devices.

2.Builder

What is it?
The Builder pattern allows a client to construct a complex object by specifying the type and content only. Construction details are hidden from the client entirely.

When you need this pattern?
To build a Composite object through some steps.

3.Prototype

What is it?
The Prototype Pattern creates new objects, but rather than creating non-initialized objects it returns objects that are initialized with values it copied from a prototype — or sample — object.

When you need this pattern?
To initialize the business objects with values that match the default values in the database. The prototype object holds the default values that are copied over into a newly created business object.

4.Singleton

What is it?
The Singleton Pattern limits the number of instances of a particular object to just one. This single instance is called the singleton.

When you need this pattern?
To reduce the global variables.
To reuse and reduce wastage of memory

How to implement Singleton in JavaScript?
The Singleton object is implemented as an immediate anonymous function. The function executes immediately by wrapping it in brackets followed by two additional brackets. It is called anonymous because it doesn’t have a name.

Structural Patterns

1. Adapter

What is it?
Translates one interface (an object’s properties and methods) to another.

When you need this pattern?
To allow programming components to work together which otherwise wouldn’t because of mismatched interfaces.

2.Composite

What is it?
Creation of objects with properties that are primitive items or a collection of objects. Each item in the collection can hold other collections themselves, creating deeply nested structures.

When you need this pattern?
To facilitate the design and construction of recursive algorithms that iterate over each object in the Composite collection.

3.Decorator

What is it?
The Decorator pattern extends (decorates) an object’s behavior dynamically. The ability to add new behavior at runtime is accomplished by a Decorator object which ‘wraps itself’ around the original object. Multiple decorators can add or override functionality to the original object.

When you need this pattern?
If you need an object with dynamic behavior.

4.Facade
What is it?
Provides an interface which shields clients from complex functionality in one or more subsystems.

When you need this pattern?
To provide a high-level interface (properties and methods) that makes a subsystem or toolkit easy to use for the client.
Ex: Express server object

Behavioral Patterns

1. Chain of Responsibility

What is it?
Provides a chain of loosely coupled objects one of which can satisfy a request.

When you need this pattern?
When we need a linear search for an object that can handle a particular request.
Ex: Event-bubbling in which an event propagates through a series of nested controls one of which may choose to handle the event.

2. Observer

What is it?
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

When you need this pattern?
To have event and event-handler paradigm
To facilitate good object-oriented design and to promote loose coupling.

3. Iterator

What is it?
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

When you need this pattern?
To effectively loop over a collection of objects.

--

--