教人做美食视频网站,安全通道入口,网络服务商包括,厦门海沧网站建设模板方法模式
#includeiostream
#includestring
using namespace std;/*案例#xff1a;写简历内容#xff1a;最近有个招聘会#xff0c;可以带上简历去应聘了。但是#xff0c;其中有一家公司不接受简历#xff0c;而是给应聘者发了一张简历表#xf…模板方法模式
#includeiostream
#includestring
using namespace std;/*案例写简历内容最近有个招聘会可以带上简历去应聘了。但是其中有一家公司不接受简历而是给应聘者发了一张简历表上面有基本信息、教育背景、工作经历等栏让应聘者按照要求填写完整。每个人拿到这份表格后就开始填写。如果用程序实现这个过程该如何做呢一种方案就是用模板方法模式定义一个操作中的算法的骨架而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
*/// 简历抽象
class Resume
{
protected:virtual void writeBasicInfo() 0;virtual void writeEducation() 0;virtual void writeWorkExperience() 0;public:// 模板方法核心void FillResume(){writeBasicInfo();writeEducation();writeWorkExperience();}virtual ~Resume() {}; // 析构函数
};// 简历A具体
class ResumeA : public Resume
{
protected:void writeBasicInfo(){cout 简历A: 基本信息, ;}void writeEducation(){cout 教育背景, ;}void writeWorkExperience(){cout 工作经验 (越详细越好). endl;}
};// 简历B具体
class ResumeB : public Resume
{
protected:void writeBasicInfo(){cout 简历B: 基本信息, ;}void writeEducation(){cout 教育背景, ;}void writeWorkExperience(){cout 工作经验 (请简要概况). endl;}
};int main()
{// 写简历AResume* r1 new ResumeA;r1-FillResume();// 写简历BResume* r2 new ResumeB;r2-FillResume();delete r1;delete r2;system(pause);return 0;
}命令模式
#includeiostream
#includestring
#includevector
using namespace std;/*案例客人点餐1. 客人发出命令让厨师做饭2. 客人发出命令让厨师取消做饭3. 客人发出命令让厨师煮面4. 客人发出命令让厨师取消煮面
*/// 厨师具体
class Chef
{
public:// 做饭void makeMeals(){cout 正在做饭中... endl;}// 取消做饭void cancelMeals(){cout 已取消做饭! endl;}// 煮面void makeNoodles(){cout 正在煮面中... endl;}// 取消煮面void cancelNoodles(){cout 已取消煮面 endl;}
};// 命令抽象
class Command
{
protected:Chef* chef; // 用来保存一个厨师public:virtual void executeCommand() 0; // 执行命令virtual void cancelCommand() 0; // 取消命令virtual ~Command() {};
};// 关于做饭的命令具体
class AboutMealsCommand : Command
{
public:// 构造函数列表初始化每构造一条命令都要对应着一个厨师让他执行这个命令AboutMealsCommand(Chef* c){this-chef c;}// 执行命令void executeCommand(){chef-makeMeals(); // 让厨师做饭}// 取消命令void cancelCommand(){chef-cancelMeals(); // 让厨师取消做饭}
};// 关于煮面的命令具体
class AboutNoodlesCommand : Command
{
public:// 构造函数列表初始化每构造一条命令都要对应着一个厨师让他执行这个命令AboutNoodlesCommand(Chef* c){this-chef c;}// 执行命令void executeCommand(){chef-makeNoodles(); // 让厨师煮面}// 取消命令void cancelCommand(){chef-cancelNoodles(); // 让厨师取消煮面}
};// 客人具体
class Customer
{
private:vectorCommand* commandQueue; // 命令队列用来存放这个顾客的一系列命令public:// 添加命令void add(Command* command){commandQueue.push_back(command); // 将当前这一条的下单命令添加到命令队列中cout 客人发出了一条命令 endl;}// 移除命令void remove(Command* command){for (auto iter commandQueue.begin(); iter ! commandQueue.end(); iter){// 将当前命令从命令队列中删除if ((*iter) command){commandQueue.erase(iter);break;}}cout 客人取消了一条命令 endl;}// 确认订单void confirm(){for (auto command : commandQueue){command-executeCommand();}commandQueue.clear();}// 取消订单void cancel(){for (auto command : commandQueue){command-cancelCommand();}commandQueue.clear();}
};int main()
{// 招牌一个厨师Chef* chef new Chef();// 制定好跟做饭相关的命令/规则具体包括做饭与取消做饭两个功能Command* mealsCommand (Command*) new AboutMealsCommand(chef);// 制定好跟煮面相关的命令/规则具体包括做饭与取消做饭两个功能Command* noodlesCommand (Command*) new AboutNoodlesCommand(chef);// 来了一位顾客Customer* customer new Customer();// 顾客下达一系列的命令想要点单customer-add(mealsCommand);customer-add(noodlesCommand);customer-remove(mealsCommand);// 顾客确认订单customer-confirm();// 顾客下达了一系列的命令想要取消点单customer-add(noodlesCommand);// 顾客取消了订单customer-cancel();delete customer;delete chef;delete mealsCommand;delete noodlesCommand;system(pause);return 0;
}责任链模式
#includeiostream
#includestring
using namespace std;/*案例员工请假内容当员工申请请假1天以内由组长批准即可处理者为组长当员工申请请假超过3天需要由经理批准处理者为经理当员工申请请假超过7天需要由老板批准处理者为老板
*/// 处理者抽象
class Handler
{
protected:Handler* nextHanler; // 维护下一个处理者的指针。万一当前处理者没有权力处理时就交给下一个处理者进行处理。public:// 构造函数列表初始化Handler() : nextHanler(nullptr) { cout 初始化成功 endl; }// 设置下一个处理者是谁如果不设置就默认没有关联void setNextHandler(Handler* next){this-nextHanler next;}// 具体的处理请求virtual void handleRequest(int days) 0;virtual ~Handler() {};
};// 组长具体
class GroupLeader : public Handler
{
public:void handleRequest(int days){cout 组长回复;if (days 1){cout 同意请假 endl;}else{cout 请假太久了你去找经理请假。 endl;if (this-nextHanler ! nullptr) nextHanler-handleRequest(days);}}
};// 经理具体
class Manager : public Handler
{
public:void handleRequest(int days){cout 经理回复;if (days 3){cout 同意请假 endl;}else{cout 请假太久了你去找老板请假。 endl;if (this-nextHanler ! nullptr) nextHanler-handleRequest(days);}}
};// 老板具体
class Boss : public Handler
{
public:void handleRequest(int days){cout 老板回复;if (days 7){cout 同意请假 endl;}else{cout 请假太久了不行 endl;if (this-nextHanler ! nullptr) nextHanler-handleRequest(days);}}
};int main()
{// 实例化一个组长、一个经理、一个老板Handler* groupLeader new GroupLeader;Handler* manager new Manager;Handler* boss new Boss;// 组装链groupLeader-setNextHandler(manager);manager-setNextHandler(boss);// 请假int days;// ------------------days 1;cout 想要请假 days 天 endl;groupLeader-handleRequest(days);// ------------------days 3;cout 想要请假 days 天 endl;groupLeader-handleRequest(days);// ------------------days 7;cout 想要请假 days 天 endl;groupLeader-handleRequest(days);// ------------------days 30;cout 想要请假 days 天 endl;groupLeader-handleRequest(days);delete groupLeader;delete manager;delete boss;system(pause);return 0;
}策略模式
#includeiostream
#includestring
using namespace std;// 策略抽象
class Strategy
{
public:virtual int execute(int left, int right) 0;
};// 加法策略具体
class Add : public Strategy
{
public:int execute(int left, int right){return left right;}
};// 减法策略具体
class Sub : public Strategy
{
public:int execute(int left, int right){return left - right;}
};// 乘法策略具体
class Mul : public Strategy
{
public:int execute(int left, int right){return left * right;}
};// 除法策略具体
class Div : public Strategy
{
public:int execute(int left, int right){if (right 0){cout 除数不能为零 endl;return 0;}return left / right;}
};// 策略容器具体
class Container
{
private:Strategy* strategy; // 维护一个策略public:// 设置策略void setStrategy(Strategy* s){this-strategy s;}// 执行某策略的功能int executeStrategy(int left, int right){return strategy-execute(left, right);}
};int main()
{// 实例化一个策略容器Container* container new Container;int left, right;char symbol;Strategy* strategy nullptr;while (true){cout (Count) ;// 获取用户输入cin left symbol right;// 根据用户选择向策略容器里面添加合适的策略switch (symbol){case :strategy new Add;container-setStrategy(strategy);break;case -:strategy new Sub;container-setStrategy(strategy);break;case *:strategy new Mul;container-setStrategy(strategy);break;case /:strategy new Div;container-setStrategy(strategy);break;}// 执行策略容器里面的策略cout container-executeStrategy(left, right) endl;delete strategy;}system(pause);return 0;
}观察者模式
#includeiostream
#includestring
#includevector
using namespace std;/*案例员工摸鱼————通过观察老板是否出现员工做出不同的反应摸鱼或努力工作
*/// 声明一个老板类
class Boss;// 实现一个员工类具体
class Employee
{
private:string m_name; // 维护员工自己的姓名public:// 构造函数初始化列表Employee(string name) : m_name(name) {};// 更新老板是否来了的动作消息做出相应的反应void updateInfomation(string info){cout m_name 收到情报 info endl;if (info 老板来了){cout -- 开启工作模式 endl;}else if (info 老板走了){cout -- 开启摸鱼模式 endl;}}
};// 实现一个老板类具体
class Boss
{
private:string information; // 保存一个老板的动作消息vectorEmployee* employeeContainer; // 员工容器用来存放老板手下的所有员工public:// 添加员工void addEmployee(Employee* employee){this-employeeContainer.push_back(employee);}// 老板设置自己的动作消息void setActionInfo(string info){this-information info; // 老板设置自己的动作消息notify(); // 发出通知老板更新了自己的动作}// 发出通知void notify(){for (auto v : employeeContainer){v-updateInfomation(this-information); // 更新员工的消息从而实现监控(观测)老板}}
};int main()
{// 实例化一个老板被观察者Boss* boss new Boss;// 实例化一些员工观察者Employee* emp1 new Employee(小明);Employee* emp2 new Employee(老张);Employee* emp3 new Employee(老李);// 让老板去招聘上面这些员工建立关联boss-addEmployee(emp1);boss-addEmployee(emp2);boss-addEmployee(emp3);// 老板设置动作消息// 当老板设置动作消息成功后将会自动发出通知更新员工的消息员工根据消息内容选择是否摸鱼boss-setActionInfo(老板来了);boss-setActionInfo(老板走了);delete boss;delete emp1;delete emp2;delete emp3;system(pause);return 0;
}访问者模式
#includeiostream
#includestring
using namespace std;/*案例这里实现一个不同职业的人去医院和餐厅的例子来说明访问者模式在小镇上有一个医院和一个餐厅每天都会有不同的人访问这两个地方由于访问者不同到这两个地方要做的事也有区别。医生去医院是为了工作给病人看病厨师去医院是为了检查身体医生去餐厅是为了吃饭厨师去餐厅是为了工作给客人烹饪菜肴。
*/// 声明地方类
class Place;// 访问者抽象访问者
class Visitor
{
public:virtual void visitHospital(Place* place) 0;virtual void visitResteraunt(Place* place) 0;virtual ~Visitor() {};
};// 地方抽象地方
class Place
{
protected:string placeName; // 保存地方的名字public:string getName() // 获得地方的名字{return this-placeName;}// 让这个地方接收访问者virtual void accept(Visitor* visitor) 0;virtual ~Place() {}
};// 医院具体地方
class Hospital : public Place
{
public:Hospital(string name){this-placeName name;}// 接受访问者的访问访问者将访问医院内部void accept(Visitor* visitor){visitor-visitHospital(this); // 访问者访问医院内部}
};// 餐馆具体地方
class Resteraunt : public Place
{
public:Resteraunt(string name){this-placeName name;}// 接受访问者的访问访问者将访问餐馆内部void accept(Visitor* visitor){visitor-visitResteraunt(this); // 访问者访问餐馆内部}
};// 医生具体访问者
class Doctor : public Visitor
{
public:void visitHospital(Place* place){cout 医生访问 place-getName() 是为了治疗病人 endl;}void visitResteraunt(Place* place){cout 医生访问 place-getName() 是为了吃一顿饭 endl;}
};// 厨师具体访问者
class Chef : public Visitor
{
public:void visitHospital(Place* place){cout 厨师访问 place-getName() 是为了治病 endl;}void visitResteraunt(Place* place){cout 厨师访问 place-getName() 是为了做菜 endl;}
};int main()
{// 实例化一个医院Place* hospital new Hospital(丽江市第一人名医院);// 实例化一个餐馆Place* resteraunt new Resteraunt(幸福餐馆);// 实例化一个医生访问者Visitor* doctor new Doctor();// 实例化一个厨师访问者Visitor* chef new Chef();// 医生访问医院医院接收医生的拜访hospital-accept(doctor);// 医生访问餐馆餐馆接收医生的拜访resteraunt-accept(doctor);// 厨师访问医院医院接收厨师的拜访hospital-accept(chef);// 厨师访问餐馆餐馆接收厨师的拜访resteraunt-accept(chef);delete hospital;delete resteraunt;delete doctor;delete chef;system(pause);return 0;
}中介者模式
#includeiostream
#includestring
using namespace std;class Role; // 声明角色// 中介者抽象中介者
class Mediator
{
protected:Role* hr;Role* student;public:void set_HR(Role* hr){this-hr hr;}void set_Student(Role* student){this-student student;}virtual void match() 0;
};// 角色抽象角色
class Role
{
protected:string name; // 角色名字string offer; // offer的岗位或内容Mediator* mediator; // 保存一个中介该角色需要求助中介public:string getName(){return this-name;}string getOffer(){return this-offer;}// 进行配对virtual void match(Role* role) 0;
};// 学生具体角色
class Student : public Role
{
public:Student(string name, string offer, Mediator* mediator){this-name name;this-offer offer;this-mediator mediator;}void match(Role* role){mediator-set_HR(role);mediator-set_Student(this);mediator-match();}
};// 人事HR具体角色
class HR : public Role
{
public:HR(string name, string offer, Mediator* mediator){this-name name;this-offer offer;this-mediator mediator;}void match(Role* role){mediator-set_HR(this);mediator-set_Student(role);mediator-match();}
};// 猎聘App具体中介者
class LiepinApp : public Mediator
{
public:void match(){cout ------------------ endl;cout hr-getName() 提供岗位 hr-getOffer() endl;cout student-getName() 需求职位 student-getOffer() endl;if (hr-getOffer() student-getOffer()){cout 配对成功 endl;}else{cout 配对失败 endl;}cout ------------------ endl;}
};int main()
{// 实例化 中介 ———— 猎聘AppMediator* app1 new LiepinApp;// 实例化 人事HRRole* hr new HR(花儿姐, 软件工程师, app1); // 使用 app1app1-set_HR(hr); // 把自己的招聘信息挂到网上// 实例化 学生Role* student new Student(小明, 机械工程师, app1); // 使用 app1app1-set_Student(student); // 把自己的求职信息挂到网上// 让 app1 进行匹配 app1-match();delete app1;delete hr;delete student;system(pause);return 0;
}备忘录模式
// 待补充状态模式
// 待补充迭代器模式
// 待补充解释器模式
// 待补充