Skip to main content

🧩 Facade Γ— Iterator

βœ… Combined Intent​

  • Use the Facade pattern to provide a simple entry point to a multi-step operation
  • Use the Iterator pattern to sequentially execute a list of tasks inside the facade

This design consolidates complex operations (like logging, user notification, validation, etc.) behind a single faΓ§ade method, while internally iterating over a collection of modular tasks.

βœ… When to Use​

  • You want to simplify the interface for a multi-step workflow
  • You want to modularize each step (logging, validation, notification) and reuse or reorder them easily
  • You need to maintain a consistent structure for running operations in sequence

βœ… UML Class Diagram​

βœ… Code Example​

interface Task {
execute(): void;
}

class LogTask implements Task {
constructor(private message: string) {}

execute(): void {
console.log(`[Log]: ${this.message}`);
}
}

class EmailTask implements Task {
constructor(private recipient: string, private content: string) {}

execute(): void {
console.log(`[Email]: To ${this.recipient} - ${this.content}`);
}
}

class TaskExecutor {
constructor(private tasks: Task[]) {}

runAll(): void {
for (const task of this.tasks) {
task.execute();
}
}
}

class UserRegistrationFacade {
registerUser(user: string): void {
const tasks: Task[] = [
new LogTask(`Registering user: ${user}`),
new EmailTask(user, "Welcome to our service!"),
new LogTask(`User registered successfully: ${user}`),
];

const executor = new TaskExecutor(tasks);
executor.runAll();
}
}

// 使用例
const facade = new UserRegistrationFacade();
facade.registerUser("taro@example.com");

βœ… Explanation​

  • UserRegistrationFacade exposes a simple registerUser() method to clients
  • Internally, it uses TaskExecutor to iterate through a list of tasks implementing a shared Task interface
  • New tasks (e.g., logging, sending notifications) can be added without modifying the facade logic

This combination provides high cohesion and low coupling, improving reusability, readability, and testability.

βœ… Summary​

  • Facade offers a simplified interface for complex operations
  • Iterator modularizes and sequences operations step-by-step
  • Perfect for workflows that need to be simple on the outside but flexible and extensible on the inside