Elegant patterns in JavaScript
Last Updated :
27 Sep, 2023
In this article, we will learn about Elegant Patterns in JavaScript, the Elegant Patterns in JavaScript explore advanced design patterns that empower developers to create maintainable, scalable, and efficient code, and JavaScript's flexible nature allows the application of the various patterns that address common software engineering challenges.
There are several methods that can be used to perform Elegant patterns in JavaScript, which are listed below:
We will explore all the above methods along with their basic implementation with the help of examples.
Approach 1: Singleton Pattern
The singleton pattern ensures that a class has only one instance and provides a global point of access to that instance and is valuable for controlling access to a shared resource or maintaining a single configuration object across an application.
Syntax:
class GFG {
constructor() {
if (GFG.instance) {
}
// code
}
}
const A = new GFG();
Example: In this example, we use Singleton pattern implementation. GFG constructor returns existing instance if present, else creates one. A and B reference the same instance. Output: true.
JavaScript
class GFG {
constructor() {
if (GFG.instance) {
return GFG.instance;
}
GFG.instance = this;
}
}
const A = new GFG();
const B = new GFG();
console.log(A === B);
Approach 2: Observer Pattern
The observer model facilitates communication between objects and here an object maintains a list of its dependents and notifies them of the any state changes.
Syntax:
class GFG {
constructor() {
}
addObserver(observer) {
}
removeObserver(observer) {
this.obs = this.obs.filter(obs =>
obs !== observer);
}
notify(data) {
this.obs.forEach(observer =>
observer.update(data));
}
// code
}
class Observer {
constructor(name) {
this.name = name;
}
}
Example: In this example, Class GFG manages observers, notifies them of data changes. Observer receives updates. Instances added, notified. Output: Observers respond to 'Update 01'.
JavaScript
class GFG {
constructor() {
this.obs = [];
}
addObserver(observer) {
this.obs.push(observer);
}
removeObserver(observer) {
this.obs = this.obs.filter(obs =>
obs !== observer);
}
notify(data) {
this.obs.forEach(observer =>
observer.update(data));
}
}
class Observer {
constructor(name) {
this.name = name;
}
update(data) {
console.log(`${this.name}
received the data: ${data}`);
}
}
const subject = new GFG();
const observer1 = new Observer('Observer 01');
const observer2 = new Observer('Observer 02');
subject.addObserver(observer1);
subject.addObserver(observer2);
subject.notify('Update 01');
OutputObserver 01
received the data: Update 01
Observer 02
received the data: Update 01
Similar Reads
String in DSA Using JavaScript A string in JavaScript is a sequence of characters enclosed in single ('), double ("), or backticks (`). Strings in JavaScript are immutable, meaning their contents cannot be changed after creation. Any operation that modifies a string actually creates a new string.Example:JavaScriptlet s = "GfG"; c
2 min read
JavaScript String Exercise JavaScript string is a primitive data type and is used for storing and manipulating a sequence of characters. It can contain zero or more characters within single or double quotes. This article contains a wide collection of JavaScript Programming examples based on String. JavaScriptlet s = "Geeksfor
7 min read
What is Math in JavaScript? JavaScript provides a built-in Math object that provides a set of methods to perform mathematical operations. These operations range from basic to more complex functions like trigonometry and logarithms. The Math object allows you to perform calculations without the need for writing custom functions
2 min read
How to write a function in JavaScript ? JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod
4 min read
Getting Started with JavaScript JavaScript is a lightweight, single-threaded, dynamically typed, interpreted programming language with object-oriented capabilities. Primarily JavaScript is used to add interactivity and dynamism to web pages. It breathes life into static HTML and CSS, enabling features like dynamic content updates,
8 min read
What is JavaScript? JavaScript is a powerful and flexible programming language for the web that is widely used to make websites interactive and dynamic. JavaScript can also able to change or update HTML and CSS dynamically. JavaScript can also run on servers using tools like Node.js, allowing developers to build entire
6 min read