JavaScript Tutorial

Design Patterns

Module, Observer, Factory, Singleton, and Proxy patterns

What are Design Patterns?

Design patterns are reusable solutions to common problems in software design. They aren't pieces of code you can just copy-paste; they are templates for how to structure your code for better scalability, maintainability, and testability.

1. The Module Pattern (ES6)

This is the most common pattern in modern JavaScript. It uses Encapsulation to keep certain functions and variables private, while only exposing a public API to the rest of the application.
javascript
// A simple Module!
export const userModule = (() => {
  let userCount = 0; // Private!

  return {
    addUser: (name) => {
        userCount++;
        console.log(`Added user: ${name}`);
    },
    getCount: () => userCount
  };
})();

2. The Singleton Pattern

The Singleton ensures that only one instance of a class ever exists throughout your entire application. This is useful for global things like a configuration object or a database connection.
javascript
class Database {
  constructor() {
    if (Database.instance) {
        return Database.instance;
    }
    Database.instance = this;
    this.connection = "Database connected!";
  }
}

// Both will point to the IDENTICAL object!
const db1 = new Database();
const db2 = new Database();
console.log(db1 === db2); // true

3. The Observer Pattern (Pub/Sub)

The Observer is a pattern where an object (the Subject) maintains a list of its dependents (the Observers) and notifies them automatically of any status changes. This is the foundation of event-driven programming!
javascript
class Subject {
  constructor() {
      this.observers = [];
  }
  
  subscribe(fn) { this.observers.push(fn); }
  
  notify(data) {
      this.observers.forEach(fn => fn(data));
  }
}

const news = new Subject();
news.subscribe(msg => console.log(`Monitor 1: ${msg}`));
news.subscribe(msg => console.log(`Monitor 2: ${msg}`));

news.notify("New Update Found!");

4. The Proxy Pattern

A Proxy acts as a middleman for another object. It allows you to intercept and control operations on that object (like getting/setting values).
javascript
const user = { name: "Alpha", age: 25 };

// Intercept all requests for names!
const userProxy = new Proxy(user, {
  get: (target, prop) => {
    console.log(`Accessing: ${prop}`);
    return target[prop];
  }
});

console.log(userProxy.name); // Accessing: name -> "Alpha"

Summary

PatternDescriptionBest Use Case...
ModulePrivate/Public segregationFile organization, Libraries
SingletonLimit instance count to ONEGlobal Config, DB Connection
ObserverOne-to-many notificationsEvents, Real-time updates
FactoryGeneric object creatorComplex object generation
ProxyObject middlemanValidation, Logging, API wrapping
MixinMulti-class functionalityReusable smaller behaviors
Best ForArchitecture, scalability, clear communication
PrototypeCloning existing objectsHigh-performance inheritance
DecoratorDynamically extend objectAdding features to a base object
FacadeSimple API to complex logicWrapping a complicated library
StrategySwappable algorithmsSelecting logic at runtime
CommandEncapsulate request as objectRedo/Undo features
MVVM / MVCLarge-scale architectureOrganizing entire apps
HooksReact's version of patternsState/Lifecycle encapsulation