Member-only story
10 Javascript Design Patterns To Improve Your Code With

There are common design patterns that you can use that will improve your code substantially. These are techniques to simplify and better structure your code in a way that makes it reusable, modular, easier to consume and test, as well as speed up your coding process.
Types of Patterns
There are three(3) types of patterns. Creational, structural, and behavioral.
- Creational — Addresses problems related to creating objects.
- Structural — Addresses the relationship between entities and how together they can compose a larger structure.
- Behavioral — Addresses how objects communicate and interact with each other.
Factory Pattern
This is one of the most common creational design patterns and one of my favorites when it comes to creating objects. It allows you to separate the implementation from the creation details of a particular object by abstracting away any complexity related to interacting with a particular API.
Another thing it allows us to do is to create an object whose class or constructor is only known at runtime. If you ever used Express.js for Node you used its app factory(createApplication) when you created the express app. The constructor that creates the app for you is created at runtime and the factory exposes just enough stuff for you to interact with it.
const express = require('express');const app = express();
Below is what the express app factory looks like on the surface. It has its internal APIs that take care of different things and then when you request an app it then goes ahead and assembles your object — hence the name factory — and returns what you asked for.
The advantage of that is you always have the same way to create the app and they can change the internal part however they like and you never need to know about it.
