The Template Method pattern locks down the structure of an algorithm in a base class but lets subclasses fill in the specific steps. The "template" is the fixed sequence; the blanks are what change between implementations.
class DataProcessor {
process() {
this.loadData();
this.processData();
this.saveData();
}
loadData() {
throw new Error("You have to implement the method loadData!");
}
processData() {
throw new Error("You have to implement the method processData!");
}
saveData() {
throw new Error("You have to implement the method saveData!");
}
}
class CSVDataProcessor extends DataProcessor {
loadData() {
console.log("Loading data from CSV file");
// Load CSV data
}
processData() {
console.log("Processing CSV data");
// Process CSV data
}
saveData() {
console.log("Saving processed data to CSV file");
// Save processed data
}
}
class JSONDataProcessor extends DataProcessor {
loadData() {
console.log("Loading data from JSON file");
// Load JSON data
}
processData() {
console.log("Processing JSON data");
// Process JSON data
}
saveData() {
console.log("Saving processed data to JSON file");
// Save processed data
}
}-
Base Class:
DataProcessor- The
DataProcessorclass contains a methodprocess()which outlines the steps for processing data:loadData(),processData(), andsaveData(). - Each of these methods (
loadData(),processData(), andsaveData()) throws an error, indicating that they must be implemented by subclasses.
- The
-
Subclass:
CSVDataProcessor- Inherits from
DataProcessor. - Implements the
loadData(),processData(), andsaveData()methods specifically for CSV data. - Each method logs a message to the console indicating its operation.
- Inherits from
-
Subclass:
JSONDataProcessor- Inherits from
DataProcessor. - Implements the
loadData(),processData(), andsaveData()methods specifically for JSON data. - Each method logs a message to the console indicating its operation.
- Inherits from
const csvProcessor = new CSVDataProcessor();
csvProcessor.process();
const jsonProcessor = new JSONDataProcessor();
jsonProcessor.process();- Instances of
CSVDataProcessorandJSONDataProcessorare created. - The
process()method is called on each instance, which in turn calls the overridden methods (loadData(),processData(), andsaveData()) in the respective subclasses.
process() always runs loadData → processData → saveData in that order: the sequence is guaranteed by the base class. Subclasses only care about their specific steps, not the overall flow. It's a simple way to enforce a pipeline without duplicating the orchestration logic in every implementation.