Design patterns are guidelines for solving common problems in software development. By learning design patterns, you can quickly and easily communicate designs to other software engineers. Here is an overview of some of the common JavaScript design patterns:
Singleton
The Singleton Pattern ensures that a class has only one instance, and provides a global access point to it. This instance can be shared throughout an application, which makes Singletons great for managing global state.
Example:
let instance;
class Example {
constructor() {
if (instance) {
throw new Error("You've already created an instance!")
}
this.example = example;
instance = this;
}
getExample() {
return this.example;
}
}
Pros to using the Singleton Pattern
✅ You can potentially save a lot of memory since you don’t have to set up memory for a new instance.
Cons to using the Singleton Pattern
❌ We no longer need to explicitly create Singletons since ES2015 Modules are Singletons by default.
❌ The global variables are accessible throughout the code. This can lead to problems like race conditions.
❌ When importing a module, it might not be obvious that the module is importing a Singleton which can lead to unexpected value changes within the Singleton.
Proxy
The Proxy pattern uses a Proxy to serve as an interface to control interactions to target objects.
Example:
const targetObject = {
name: "User",
message: "hello world"
}
const handler = {
get(targetObject, prop, receiver) {
return "!";
}
}
const proxy = new Proxy(targetObject, handler);
console.log(proxy.message); // !
Pros to using the Proxy Pattern
✅ It’s easier to add functionality with specific objects (i.e. logging, debugging, notifications, validation, etc).
Cons to using the Proxy Pattern
❌ Could cause performance issues from executing handlers on every object.
Observer
The Observer Pattern is the most commonly used design pattern in the real world. It defines a one-to-many dependency between objects so that when one object changes its state, all of its dependents are notified and updated automatically.
Example:
class Observable {
constructor() {
this.observers = [];
}
subscribe(exampleFunction) {
this.observers.push(exampleFunction);
}
unsubscribe(exampleFunction) {
this.observers = this.observers.filter((observer) => observer !== exampleFunction);
}
notify(data) {
this.observers.forEach((observer) => observer(data));
}
}
Pros to using the Observer Pattern
✅ Observer objects (that handle the received data) can be decoupled/coupled easily with the observable object (monitors the events).
Cons to using the Observer Pattern
❌ Could potentially have performance issues because of the time it takes to notify all subscribers (i.e. if there are too many subscribers, if the logic becomes too complicated).
Factory
The Factory Pattern wraps a constructor for different types of objects and returns instances of the objects.
Example:
const createItem = (name, message) => ({
createdAt: Date.now(),
name,
message,
quote: `${name} said "${message}"`,
});
createItem("User", "Hello World!");
Pros to using the Factory Pattern
✅ Keeps code DRY and is handy when we need to create several objects that share the same properties.
Cons to using the Factory Pattern
❌ Might be more memory efficient to create new instances instead of new objects.
Prototype
The Prototype Pattern creates new objects and returns objects that are initialized with values copied from the prototype. It is a helpful way to share properties among many objects of the same type.
Example:
const createUser = (name, message) => ({
name,
message,
speak() {
console.log(`${name} said "${message}"`);
},
walking() {
console.log(`${name} is walking!`);
},
});
const user1 = createUser("Jill", "Hello world!");
const user2 = createUser("Jack", "Good morning!");
Pros to using the Prototype Pattern
✅ More efficient with memory since we can access properties that aren’t defined directly on the object. This allows us to avoid duplication of properties and methods.
Cons to using the Prototype Pattern
❌ Issues with readability – if a class is extended quite a few times, it can be hard to know where specific properties come from.
Additional Resources
patterns.dev