Specification Pattern
ELI5 — The Vibe Check
The specification pattern turns business rules into reusable, composable objects. Instead of if-statements scattered everywhere, you create specs like 'IsActive', 'HasPremiumPlan', 'CreatedThisMonth' and combine them with AND, OR, NOT. It's like building filters from LEGO blocks.
Real Talk
The specification pattern encapsulates business rules as first-class objects that can be combined using boolean logic (AND, OR, NOT). Each specification implements a single rule with an isSatisfiedBy method. Specifications can be used for validation, querying, and filtering while keeping business logic DRY and domain-expressive.
Show Me The Code
class IsActive {
isSatisfiedBy(user: User) { return user.status === 'active'; }
}
class HasPremiumPlan {
isSatisfiedBy(user: User) { return user.plan === 'premium'; }
}
const canAccessFeature = new AndSpec(new IsActive(), new HasPremiumPlan());
if (canAccessFeature.isSatisfiedBy(user)) { /* grant access */ }
When You'll Hear This
"Compose specifications to build complex business rules from simple building blocks." / "The specification pattern keeps our eligibility logic testable and reusable."
Related Terms
Design Pattern
Design patterns are like recipe cards for solving common coding problems.
Domain-Driven Design (DDD)
DDD says your code should speak the same language as the business.
Strategy Pattern
You're writing a sorter and want to sort by price, name, or date depending on user choice.
Validation
Validation is your backend's bouncer. Before any data gets into the database, the bouncer checks it: 'Is this email actually an email?