GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing www.tutorialspoint.com — Template Method Design Pattern in C++
Selected Reading
Template Method Design Pattern in C++The Template Method Design Pattern is a way to set up a common process that can be shared by different classes. It gives a main method called the template method that explains the steps to complete a task. Other classes can then fill in the details for some of those steps in their own way, without changing the main process. This helps in reusing code and keeping things simple as well as consistent. This pattern is helpful when you have a task that follows the same steps every time, but a few steps might need to be done differently. You can keep the main steps in one place and let other classes change only the parts they need. This makes the program easy to read and simple to manage. For example, think of a program that shows how to prepare a meal. The template method might describe the main steps : gathering ingredients, cooking, and serving. Now, other classes can change how these steps are done to make different meals like breakfast, lunch, or dinner. The main idea stays the same, but each meal can have its own special details.
Key Parts of the Template Method Design PatternThe Template Method Design Pattern has following components that work together −
C++ Implementation of the Template Method Design PatternLet's learn how to use the Template Method pattern in C++ with a simple example. We'll make a program that shows how to prepare different kinds of meals by following the same main steps. Steps to use the Template Method pattern in C++Following are the steps to implement the Template Method Design Pattern in C++
This way, you can keep the main process in one place and still make it easy to add new meal types later without changing the whole program. C++ Code to Implement Template Method Design PatternBelow is a simple C++ code example demonstrating the Template Method Design Pattern for preparing different types of meals. #include <iostream>
using namespace std;
// Abstract class defining the template method
class Meal {
public:
// Template method
void prepareMeal() {
gatherIngredients();
cook();
serve();
}
// Abstract methods to be implemented by subclasses
virtual void gatherIngredients() = 0;
virtual void cook() = 0;
// Concrete method
void serve() {
cout << "Serving the meal!" << endl;
}
};
// Concrete subclass for preparing a breakfast meal
class Breakfast : public Meal {
public:
void gatherIngredients() override {
cout << "Gathering ingredients for breakfast: eggs, bread, and coffee." << endl;
}
void cook() override {
cout << "Cooking breakfast: frying eggs and toasting bread." << endl;
}
};
// Concrete subclass for preparing a lunch meal
class Lunch : public Meal {
public:
void gatherIngredients() override {
cout << "Gathering ingredients for lunch: chicken, rice, and vegetables." << endl;
}
void cook() override {
cout << "Cooking lunch: grilling chicken and steaming vegetables." << endl;
}
};
int main() {
Meal* breakfast = new Breakfast();
breakfast->prepareMeal();
cout << endl;
Meal* lunch = new Lunch();
lunch->prepareMeal();
// Clean up
delete breakfast;
delete lunch;
return 0;
}
Following is the output of the above code − Gathering ingredients for breakfast: eggs, bread, and coffee. Cooking breakfast: frying eggs and toasting bread. Gathering ingredients for lunch: chicken, rice, and vegetables. Cooking lunch: grilling chicken and steaming vegetables. Serving the meal! In this example, we have an abstract class called Meal. It has a main method named prepareMeal() that shows the full process of making a meal. The method includes three main steps — gathering ingredients, cooking, and serving. The subclasses Breakfast and Lunch each add their own way of doing the steps. They give their own details for how to gather the ingredients and how to cook the food. When we run the prepareMeal() method for each subclass, it follows the same main steps but changes the small details depending on the type of meal. This lets us keep the main process the same while still making each meal a little different. Pros and Cons of the Template Method Design PatternLike any design pattern, the Template Method Design Pattern has its advantages and disadvantages. Here are some of the main pros and cons −
Real-World Applications of the Template Method Design PatternThe Template Method Design Pattern is widely used in various real-world applications. Here are some common scenarios where this pattern might be useful −
ConclusionThe Template Method Design Pattern is a simple and practical way to keep one common process while letting different parts change only when it is needed. It helps you in reusing code, keeping your program organized, and make complex tasks easier to handle without repeating the same logic everywhere. Advertisements
| ||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |