What is a Singleton?
What is an Adapter Pattern
What is a Chain of Responsibility Pattern?
Pitfalls:
Mitigation:
What are the pitfalls of the Memento Pattern?
class Singleton {
private static instance: Singleton;
private constructor() {}
public static getInstance(): Singleton {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
}
return Singleton.instance;
}
}
// Usage
const s1 = Singleton.getInstance();
const s2 = Singleton.getInstance();
console.log(s1 === s2); // trueWhat is a Singleton Pattern?
What is a Factory Method?
What is a Bridge Pattern?
What is a Command Pattern?
Pitfalls:
Mitigation:
What are the pitfalls of the Observer Pattern?
// Abstract Product
interface Animal {
speak(): string;
}
// Concrete Products
class Dog implements Animal {
speak(): string {
return "Woof!";
}
}
class Cat implements Animal {
speak(): string {
return "Meow!";
}
}
class AnimalFactory {
static getAnimal(type: string): Animal {
if (type === "dog") {
return new Dog();
} else if (type === "cat") {
return new Cat();
}
throw new Error("Animal type not supported.");
}
}
// Usage
const dog = AnimalFactory.getAnimal("dog");
console.log(dog.speak()); // "Woof!"
const cat = AnimalFactory.getAnimal("cat");
console.log(cat.speak()); // "Meow!"What is the Factory Method Pattern?
What is a Builder Pattern
What is a Composite Pattern?
What is an Interpreter?
Pitfalls:
Mitigation:
What are the Pitfalls of the State Pattern
// Subject
class Subject {
private observers: Observer[] = [];
addObserver(observer: Observer): void {
this.observers.push(observer);
}
removeObserver(observer: Observer): void {
this.observers = this.observers.filter(o => o !== observer);
}
notifyObservers(): void {
this.observers.forEach(observer => observer.update());
}
}
interface Observer {
update(): void;
}
class ConcreteObserver implements Observer {
constructor(private name: string) {}
update(): void {
console.log(`${this.name} has been notified!`);
}
}
// Usage
const subject = new Subject();
const observer1 = new ConcreteObserver("Observer 1");
const observer2 = new ConcreteObserver("Observer 2");
subject.addObserver(observer1);
subject.addObserver(observer2);
subject.notifyObservers();
// Output:
// Observer 1 has been notified!
// Observer 2 has been notified!What is the Observer Pattern?
What is a Prototype Pattern
What is a Decorator Pattern?
What is an Iterator Pattern?
Pitfalls:
Mitigation:
What are the pitfalls of the Strategy Pattern?
interface PaymentStrategy {
pay(amount: number): void;
}
class CreditCardPayment implements PaymentStrategy {
pay(amount: number): void {
console.log(`Paid $${amount} using Credit Card.`);
}
}
class PayPalPayment implements PaymentStrategy {
pay(amount: number): void {
console.log(`Paid $${amount} using PayPal.`);
}
}
// Context
class PaymentContext {
private strategy: PaymentStrategy;
constructor(strategy: PaymentStrategy) {
this.strategy = strategy;
}
setStrategy(strategy: PaymentStrategy): void {
this.strategy = strategy;
}
executePayment(amount: number): void {
this.strategy.pay(amount);
}
}
// Usage
const context = new PaymentContext(new CreditCardPayment());
context.executePayment(100); // "Paid $100 using Credit Card."
context.setStrategy(new PayPalPayment());
context.executePayment(200); // "Paid $200 using PayPal."What is the Strategy Pattern?
What is an Abstract Factory?
What is a Facade?
What is a Mediator Pattern?
Pitfalls:
Mitigation:
What are the pitfalls of the Template Method Pattern?
// Abstract Class
abstract class DataProcessor {
process(): void {
this.loadData();
this.processData();
this.saveData();
}
protected abstract loadData(): void;
protected abstract processData(): void;
protected abstract saveData(): void;
}
// Concrete Class
class CSVProcessor extends DataProcessor {
protected loadData(): void {
console.log("Loading CSV data...");
}
protected processData(): void {
console.log("Processing CSV data...");
}
protected saveData(): void {
console.log("Saving CSV data...");
}
}
// Usage
const processor = new CSVProcessor();
processor.process();
// Output:
// Loading CSV data...
// Processing CSV data...
// Saving CSV data...What is the Template Method Pattern?