目录
模板方法模式(Template Method Pattern)
实际应用
数据处理流程
在线教育系统的课程模板
软件开发生命周期
总结
模板方法模式(Template Method Pattern)
模板方法模式是一种行为设计模式,它定义了一个操作的算法骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变算法结构的情况下重新定义算法中的某些步骤。
实际应用
数据处理流程
-- 有一个数据处理流程,该流程包含读取数据、处理数据和保存数据的步骤。
#include <iostream>
#include <vector>// 抽象基类
class DataProcessor {
public:// 模板方法void process() {readData();processData();saveData();}// 基类定义的钩子方法(可选)virtual void readData() = 0;virtual void processData() = 0;virtual void saveData() {std::cout << "Saving data to default location." << std::endl;}virtual ~DataProcessor() = default;
};// 具体实现类:处理CSV数据
class CSVDataProcessor : public DataProcessor {
public:void readData() override {std::cout << "Reading data from CSV file." << std::endl;}void processData() override {std::cout << "Processing CSV data." << std::endl;}void saveData() override {std::cout << "Saving processed CSV data to database." << std::endl;}
};// 具体实现类:处理JSON数据
class JSONDataProcessor : public DataProcessor {
public:void readData() override {std::cout << "Reading data from JSON file." << std::endl;}void processData() override {std::cout << "Processing JSON data." << std::endl;}void saveData() override {std::cout << "Saving processed JSON data to cloud storage." << std::endl;}
};// 客户端代码
int main() {CSVDataProcessor csvProcessor;JSONDataProcessor jsonProcessor;std::cout << "Processing CSV data:" << std::endl;csvProcessor.process();std::cout << "\nProcessing JSON data:" << std::endl;jsonProcessor.process();return 0;
}
在线教育系统的课程模板
-- 在线教育系统,其中课程模板的每种类型的课程都有不同的实现方式,例如录制课程和现场课程。
#include <iostream>// 抽象基类
class OnlineCourse {
public:// 模板方法void createCourse() {createContent();deliverContent();provideFeedback();}// 基类定义的钩子方法(可选)virtual void createContent() = 0;virtual void deliverContent() = 0;virtual void provideFeedback() {std::cout << "Providing standard feedback." << std::endl;}virtual ~OnlineCourse() = default;
};// 具体实现类:录制课程
class RecordedCourse : public OnlineCourse {
public:void createContent() override {std::cout << "Creating recorded content." << std::endl;}void deliverContent() override {std::cout << "Delivering recorded content." << std::endl;}void provideFeedback() override {std::cout << "Providing feedback for recorded content." << std::endl;}
};// 具体实现类:现场课程
class LiveCourse : public OnlineCourse {
public:void createContent() override {std::cout << "Preparing live session content." << std::endl;}void deliverContent() override {std::cout << "Delivering live session." << std::endl;}void provideFeedback() override {std::cout << "Providing live session feedback." << std::endl;}
};// 客户端代码
int main() {RecordedCourse recordedCourse;LiveCourse liveCourse;std::cout << "Creating Recorded Course:" << std::endl;recordedCourse.createCourse();std::cout << "\nCreating Live Course:" << std::endl;liveCourse.createCourse();return 0;
}
软件开发生命周期
-- 简单的软件开发生命周期模板,该模板包含需求分析、设计、开发和测试等步骤。
#include <iostream>// 抽象基类
class SoftwareDevelopmentLifecycle {
public:// 模板方法void develop() {requirementsAnalysis();design();development();testing();deployment();}// 基类定义的钩子方法(可选)virtual void requirementsAnalysis() = 0;virtual void design() = 0;virtual void development() = 0;virtual void testing() = 0;virtual void deployment() {std::cout << "Deploying software to production." << std::endl;}virtual ~SoftwareDevelopmentLifecycle() = default;
};// 具体实现类:Web项目
class WebProject : public SoftwareDevelopmentLifecycle {
public:void requirementsAnalysis() override {std::cout << "Gathering requirements for web project." << std::endl;}void design() override {std::cout << "Designing web project architecture." << std::endl;}void development() override {std::cout << "Developing web application." << std::endl;}void testing() override {std::cout << "Testing web application." << std::endl;}void deployment() override {std::cout << "Deploying web application to cloud server." << std::endl;}
};// 具体实现类:移动应用项目
class MobileAppProject : public SoftwareDevelopmentLifecycle {
public:void requirementsAnalysis() override {std::cout << "Gathering requirements for mobile app project." << std::endl;}void design() override {std::cout << "Designing mobile app architecture." << std::endl;}void development() override {std::cout << "Developing mobile application." << std::endl;}void testing() override {std::cout << "Testing mobile application." << std::endl;}void deployment() override {std::cout << "Deploying mobile application to app store." << std::endl;}
};// 客户端代码
int main() {WebProject webProject;MobileAppProject mobileAppProject;std::cout << "Developing Web Project:" << std::endl;webProject.develop();std::cout << "\nDeveloping Mobile App Project:" << std::endl;mobileAppProject.develop();return 0;
}
总结
模板方法模式通过定义算法骨架,并将具体步骤延迟到子类中,使得算法结构的变化独立于具体实现,可以提高代码的复用性和维护性。