Advanced Design Patterns

Master complex implementation strategies and architectural solutions for professional software development challenges.
🧠 Start Learning

.Observable Pattern

Enables a subscription model where objects can notify other objects about state changes.

class Observable {
  constructor() {
    this.subscribers = [];
  }
  subscribe(fn) { this.subscribers.push(fn); }
  notify(msg) { 
    this.subscribers.forEach(fn => fn(msg));
    console.log(`Notified ${this.subscribers.length} subscribers:`);
  }
}

const log = new Observable();
log.subscribe(m => console.log('Message:', m));
log.notify("System State Updated");

Strategy Pattern

Defines interchangeable algorithms to perform a specific task.

const paymentStrategies = {
  creditCard: amount => `Processed $${amount} via credit card`,
  paypal: amount => `Processed $${amount} via PayPal`,
  bitcoin: amount => `Processed $${amount} via Bitcoin`
};

class PaymentProcessor {
  constructor(strategy = 'creditCard') {
    this.strategy = strategy;
  }
  setStrategy(strategy) { this.strategy = strategy; }
  pay(amount) { 
    const strategy = paymentStrategies[this.strategy];
    console.log(strategy(amount));
  }
}

const processor = new PaymentProcessor('paypal');
processor.pay(100);
processor.setStrategy('bitcoin');
processor.pay(50);

Decorator Pattern

Add responsibilities to objects dynamically without creating subclasses.

{`function withLogging(target, propertyKey, descriptor) {
  const originalMethod = descriptor.value;
  descriptor.value = (...args) => {
    console.log(\`Calling \${propertyKey} with args:\`, args);
    const result = originalMethod.apply(this, args);
    console.log(\`Result: \${result}\`);
    return result;
  };
  return descriptor;
}

class Calculator {
  @withLogging
  add(a, b) { return a + b; }
  @withLogging
  multiply(a, b) { return a * b; }
}

const calc = new Calculator();
calc.add(10, 5);
calc.multiply(3, 7);`}