How do GoF patterns apply in functional programming?
Answer
Functional programming languages make many GoF patterns either trivial or unnecessary by using first-class functions and higher-order functions. Strategy becomes passing a function as an argument — no interface or class hierarchy needed. Command becomes a closure that captures context. Template Method becomes a higher-order function that accepts functions for the variable steps. Observer becomes subscribing a callback function or a reactive stream operator. Iterator is built into functional languages as lazy sequences or generators. Decorator maps to function composition — f(g(x)). Patterns like Singleton are avoided by passing dependencies explicitly (pure functions). Factory Method becomes a function that returns a value. The patterns that survive in FP tend to be higher-level architectural ones (Pipeline, Monad as a pattern) rather than the OOP-specific ones the GoF book addresses.
Previous
What criteria should guide pattern selection?
Next
What is the difference between dependency injection and the Service Locator pattern?
More Design Patterns (Gang of Four) Questions
View all →- Advanced How do you implement a thread-safe Singleton using double-checked locking?
- Advanced How do the SOLID principles relate to GoF patterns?
- Advanced What is the difference between GoF anti-patterns and the patterns themselves?
- Advanced How are GoF patterns used in modern frameworks like Spring and Django?
- Advanced What criteria should guide pattern selection?