Template Method: Define the skeleton of an algorithm in a base class, deferring some steps to subclasses — subclasses override specific steps without changing the algorithm's structure. The Template Method pattern defines the outline of an algorithm in a base class, and lets subclasses fill in specific steps. The base class decides the order; subclasses decide the details.
An import process might always read a file, validate rows, save them and send a report. A CSV import and an Excel import override only the reading step, while the rest stays shared.
What should you keep in mind with Template Method?
It avoids duplicating the overall flow in every variant. The downside is inheritance: subclasses depend closely on the base class, and the flow is split across files. An alternative is composition, where the shared flow receives the varying parts as objects or functions. Laravel's console commands and form requests are everyday examples of the template method idea, with methods like handle() and rules() filled in by you.
Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use this skill when designing clean architecture for a new microservice, when refactoring a monolith to use bounded contexts, when implementing hexagonal or onion architecture patterns, or when debugging dependency cycles between application layers.
'Cloud design patterns for distributed systems architecture covering 42 industry-standard patterns across reliability, performance, messaging, security, and deployment categories. Use when designing, reviewing, or implementing distributed system architectures.'