Design patterns in Javascript

Object Creation Patterns:

1. Abstract factory


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.


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

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.

To build a Composite object through some steps.

3.Prototype

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.

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

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

To reduce the global variables.
To reuse and reduce wastage of memory


The Singleton object is implemented as an . 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

Translates one interface (an object’s properties and methods) to another.

To allow programming components to work together which otherwise wouldn’t because of mismatched interfaces.

2.Composite

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.

To facilitate the design and construction of recursive algorithms that iterate over each object in the Composite collection.

3.Decorator

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.

If you need an object with dynamic behavior.

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

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

Provides a chain of loosely coupled objects one of which can satisfy a request.

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

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

To have event and event-handler paradigm
To facilitate good object-oriented design and to promote loose coupling.

3. Iterator

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

To effectively loop over a collection of objects.

Suffering from Knowledge Quest

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store