Skip to content
AgileLogics
4 min read

Design Patterns

Prerequisites: Classes and objects

A design pattern is a named, reusable solution to a recurring design problem. One of the most common in JavaScript modules:

let instance = null;

export function getDatabaseConnection() {
    if (!instance) {
        instance = createConnection();
    }
    return instance;
}

This is the Singleton pattern — a module's top-level variables are only initialized once and shared by every file that imports the module, which naturally gives this pattern "for free" in JavaScript.