当前位置:   article > 正文

【设计模式学习笔记】模板模式、命令模式、责任链模式、策略模式案例详解(C++实现)_开发 模板 责任链

开发 模板 责任链

目录

一、模板模式

1. 什么是模板模式

2. 模板模式的案例

二、命令模式

1. 什么是命令模式

2. 命令模式的案例

三、责任链模式

1.什么是责任链模式

2. 责任链模式案例

四、策略模式

1. 什么是策略模式

2. 策略模式的案例


一、模板模式

1. 什么是模板模式

Template Pattern,模板方法模式,是一种行为型模式。通过模板模式可以把特定步骤的算法接口定义在抽象基类中,通过子类继承对抽象算法进行不同的实现来达到改变算法行为的目的。通俗来讲就是,在抽象类中定义好算法步骤并统一接口,在子类中实现接口,这就实现了算法操作步骤和算法实现的解耦合。模板模式一般应用于,具有同样的操作步骤,但是这些操作的细节不同的场景。

  • AbstractClass:定义了算法的框架和步骤;
  • ConcreteClass:实现AbstractClass中的方法;

 2. 模板模式的案例

我们把穿衣服看做一个固定流程,先穿外套,再穿裤子,最后穿鞋

  1. class WearClothes //穿衣服
  2. {
  3. public:
  4. virtual void wear_coat() = 0;
  5. virtual void wear_pants() = 0;
  6. virtual void wear_shoe() = 0;
  7. public:
  8. void wear_order() //穿衣服的顺序已经提前确定好了---模板
  9. {
  10. wear_coat(); //先穿外套
  11. wear_pants(); //再穿裤子
  12. wear_shoe(); //最后穿鞋
  13. }
  14. };

 然后定义穿睡衣,穿西装两个类,穿衣服的顺序都是固定的,但是穿的衣服有所不同

  1. class WearSuit : public WearClothes //穿西装
  2. {
  3. virtual void wear_coat()
  4. {
  5. cout << "穿西服外套" << endl;
  6. }
  7. virtual void wear_pants()
  8. {
  9. cout << "穿西服裤子" << endl;
  10. }
  11. virtual void wear_shoe()
  12. {
  13. cout << "穿小皮鞋" << endl;
  14. }
  15. };
  16. class WearPajamas : public WearClothes //穿睡衣
  17. {
  18. virtual void wear_coat()
  19. {
  20. cout << "穿睡衣" << endl;
  21. }
  22. virtual void wear_pants()
  23. {
  24. cout << "穿睡裤" << endl;
  25. }
  26. virtual void wear_shoe()
  27. {
  28. cout << "穿拖鞋" << endl;
  29. }
  30. };

最后客户端执行穿衣服操作

  1. int main()
  2. {
  3. WearClothes* wear = NULL;
  4. //穿西装应酬
  5. wear = new WearSuit;
  6. wear->wear_order();
  7. delete wear;
  8. //穿睡衣睡觉
  9. wear = new WearPajamas;
  10. wear->wear_order();
  11. delete wear;
  12. system("pause");
  13. return 0;
  14. }

 二、命令模式

1. 什么是命令模式

Command Pattern,命令模式,是一种行为型设计模式。命令模式就是把命令对象、命令的创建者、命令的调用者、命令的执行者(接收者)分离,首先看一个命令模式中的四个角色:

  • Command:抽象命令,定义了操作的接口,把命令封装成一个类,通过继承在ConcreteCommand中来实现具体的命令操作;
  • ConcreteCommand:具体命令,实现抽象命令的接口,是被命令调用者Invoker调用的主体,并且包含了一个对命令接收者Receiver的引用;
  • Receiver:命令的执行者,收到命令后执行相应的操作;
  • Invoker:命令的调用者,客户创建命令,并通过Incoker去调用命令,Invoker包含了对Command的引用,并负责去调用命令中的操作;

 2. 命令模式的案例

以办理银行业务为例,首先创建一个银行职员作为命令接收者,他可以执行存款取款操作。

  1. //Receiver
  2. class Banker
  3. {
  4. public:
  5. void saving_money()
  6. {
  7. cout << "办理存款业务" << endl;
  8. }
  9. void withdraw_money()
  10. {
  11. cout << "办理取款业务" << endl;
  12. }
  13. };

 创建一个命令抽象类,并通过继承实现一个存款命令类和一个取款命令类,这两个具体命令方法分别可以调用命令执行者的存款操作和取款操作。

  1. class Command
  2. {
  3. public:
  4. virtual void conduct_business() = 0;
  5. };
  6. class SaveCommand : public Command
  7. {
  8. private:
  9. Banker* bker;
  10. public:
  11. SaveCommand(Banker* bker)
  12. {
  13. this->bker = bker;
  14. }
  15. virtual void conduct_business()
  16. {
  17. this->bker->saving_money();
  18. }
  19. };
  20. class WithdrowCommand : public Command
  21. {
  22. private:
  23. Banker* bker;
  24. public:
  25. WithdrowCommand(Banker* bker)
  26. {
  27. this->bker = bker;
  28. }
  29. virtual void conduct_business()
  30. {
  31. this->bker->withdraw_money();
  32. }
  33. };

创建一个命令调用者,它可以调用命令对象(命令调用者Invoker中包含了命令Command的引用,它可以调用命令,而具体命令ConcreteCommand中包含了命令接收者Receiver的引用,具体命令可以调用Receiver的操作,客户通过命令调用者Invoker来命令Receiver进行相应操作)。

  1. //Invoker
  2. class Manager
  3. {
  4. private:
  5. Command* com;
  6. public:
  7. Manager(Command* com)
  8. {
  9. this->com = com;
  10. }
  11. void order()
  12. {
  13. com->conduct_business();
  14. }
  15. };

最后客户端创建命令,并通过Invoker来调用命令,使Receiver执行相应操作

  1. int main()
  2. {
  3. Manager* m = NULL;
  4. Command* com = NULL;
  5. Banker* bker = NULL;
  6. bker = new Banker;
  7. com = new SaveCommand(bker); //存款命令
  8. m = new Manager(com);
  9. m->order();
  10. delete com;
  11. delete m;
  12. com = new WithdrowCommand(bker); //取款命令
  13. m = new Manager(com);
  14. m->order();
  15. delete m;
  16. delete com;
  17. delete bker;
  18. system("pause");
  19. return 0;
  20. }

 三、责任链模式

1.什么是责任链模式

Chain of Responsibility Pattern,CoR责任链模式,是行为型设计模式之一。责任链模式就像一个链表,将对象连成一个链式结构,并沿着这条链传递请求,直到请求被某个对象处理。在责任链模式中,客户端只要把请求放到对象链上即可,不需关心请求的传递过程和处理细节,实现了请求发送和请求处理的解耦合。 

  • Handler:抽象处理者,定义了处理请求的接口,并包含一个指向下一个对象的指针;
  • ConcreteHandler:具体处理者,负责处理请求或把请求沿着对象链传递给下一个具体处理者;

 2. 责任链模式案例

定义抽象处理者和具体处理者

  1. class Handler
  2. {
  3. protected: //供子类使用
  4. Handler* next;
  5. public:
  6. virtual void perform_task() = 0; //统一的任务接口
  7. Handler* set_next(Handler* next) //设置下一个要执行的任务
  8. {
  9. this->next = next;
  10. return this->next;
  11. }
  12. };
  13. class Task1 : public Handler
  14. {
  15. public:
  16. virtual void perform_task()
  17. {
  18. cout << "任务 1 执行" << endl;
  19. if (next != NULL) //如果有下一个任务,则执行
  20. {
  21. next->perform_task();
  22. }
  23. }
  24. };
  25. class Task2 : public Handler
  26. {
  27. public:
  28. virtual void perform_task()
  29. {
  30. cout << "任务 2 执行" << endl;
  31. if (next != NULL)
  32. {
  33. next->perform_task();
  34. }
  35. }
  36. };
  37. class Task3 : public Handler
  38. {
  39. public:
  40. virtual void perform_task()
  41. {
  42. cout << "任务 3 执行" << endl;
  43. if (next != NULL)
  44. {
  45. next->perform_task();
  46. }
  47. }
  48. };

客户端发出请求

  1. int main()
  2. {
  3. Handler* task1 = NULL;
  4. Handler* task2 = NULL;
  5. Handler* task3 = NULL;
  6. task1 = new Task1;
  7. task2 = new Task2;
  8. task3 = new Task3;
  9. //任务流程:task1 -> task2 -> task3 -> 结束
  10. cout << "任务流程:task1 -> task2 -> task3 -> 结束" << endl;
  11. task1->set_next(task2);
  12. task2->set_next(task3);
  13. task3->set_next(NULL);
  14. task1->perform_task();
  15. cout << "===================================" << endl;
  16. //改变流程
  17. cout << "任务流程:task3 -> task2 -> task1 -> 结束" << endl;
  18. task1->set_next(NULL);
  19. task2->set_next(task1);
  20. task3->set_next(task2);
  21. task3->perform_task();
  22. cout << "===================================" << endl;
  23. delete task3;
  24. delete task2;
  25. delete task1;
  26. system("pause");
  27. return 0;
  28. }

四、策略模式

1. 什么是策略模式

Strategy Pattern,策略模式,行为型模式之一。策略模式可以定义一个算法族,把一系列算法封装起来并提供一个统一接口,这样算法之间的切换或其他变化不会影响客户端。其关键在于,把算法的抽象接口封装在一个类中,算法的实现由具体策略类来实现,,而算法的选择由客户端决定。

  • Strategy:抽象策略类,定义算法族的统一接口;
  • ConcreteStrategy:具体策略类,实现了具体的算法操作;
  • Context:上下文,包含一个策略类的引用,根据不同策略执行不同操作,策略的选择由客户端决定;

 2. 策略模式的案例

定义一个排序算法策略

  1. class Strategy //策略
  2. {
  3. public:
  4. virtual void sort() = 0;
  5. };
  6. class SelectSort : public Strategy
  7. {
  8. public:
  9. virtual void sort()
  10. {
  11. cout << "选择排序算法" << endl;
  12. }
  13. };
  14. class InsertSort : public Strategy
  15. {
  16. public:
  17. virtual void sort()
  18. {
  19. cout << "插入排序算法" << endl;
  20. }
  21. };

定义上下文

  1. class Context
  2. {
  3. private:
  4. Strategy* m_strategy;
  5. public:
  6. void set_strategy(Strategy* m_strategy)
  7. {
  8. this->m_strategy = m_strategy;
  9. }
  10. void execute_strategy()
  11. {
  12. this->m_strategy->sort();
  13. }
  14. };

客户端选择具体排序算法

  1. int main()
  2. {
  3. Strategy* s1 = NULL;
  4. Context* c = NULL;
  5. c = new Context;
  6. cout << "========================" << endl;
  7. //使用选择排序算法进行排序
  8. cout << "使用选择排序算法进行排序" << endl;
  9. s1 = new SelectSort;
  10. c->set_strategy(s1);
  11. c->execute_strategy();
  12. delete s1;
  13. cout << "========================" << endl;
  14. cout << "使用插入排序算法进行排序" << endl;
  15. s1 = new InsertSort;
  16. c->set_strategy(s1);
  17. c->execute_strategy();
  18. delete s1;
  19. delete c;
  20. cout << "========================" << endl;
  21. system("pause");
  22. return 0;
  23. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/808304
推荐阅读
  

闽ICP备14008679号