• 设计模式-责任链-笔记


    动机(Motivation)

    在软件构建过程中,一个请求可能被多个对象处理,但是每个请求在运行时只能有个接受者,如果显示指定,将必不可少地带来请求者与接受者的紧耦合。

    如何使请求的发送者不需要指定具体的接受者?让请求的接受者自己在运行是决定来处理请求,从而两者解耦。

    模式定义:

    使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有一个请求处理他为止。

    1. #include
    2. #include
    3. using namespace std;
    4. enum class RequestType {
    5. REQ_HANDLER1,
    6. REQ_HANDLER2,
    7. REQ_HANDLER3,
    8. };
    9. class Request {
    10. string description;
    11. RequestType reqType;
    12. public:
    13. Request(const string& desc, RequestType type)
    14. : description(desc), reqType(type) {}
    15. RequestType getReqType() const { return reqType; }
    16. const string& getDescription() const { return description; }
    17. };
    18. class ChainHandler {
    19. ChainHandler* nextChain;
    20. void sendRequestToNextHandler(const Request& req) {
    21. if (nullptr != nextChain) {
    22. nextChain->handle(req);
    23. }
    24. }
    25. protected:
    26. virtual bool canHandleRequest(const Request& req) = 0;
    27. virtual void processRequest(const Request& req) = 0;
    28. public:
    29. ChainHandler() { nextChain = nullptr; }
    30. void setNextChain(ChainHandler* next) { nextChain = next; }
    31. void handle(const Request& req) {
    32. if (canHandleRequest(req)) {
    33. processRequest(req);
    34. }
    35. else{
    36. sendRequestToNextHandler(req);
    37. }
    38. }
    39. };
    40. class Handler1 : public ChainHandler {
    41. protected:
    42. virtual bool canHandleRequest(const Request& req) override {
    43. return req.getReqType() == RequestType::REQ_HANDLER1;
    44. }
    45. virtual void processRequest(const Request& req) override {
    46. cout << "Handler1 is handle request: " << req.getDescription() << endl;
    47. }
    48. };
    49. class Handler2 : public ChainHandler {
    50. protected:
    51. virtual bool canHandleRequest(const Request& req) override {
    52. return req.getReqType() == RequestType::REQ_HANDLER2;
    53. }
    54. virtual void processRequest(const Request& req) override {
    55. cout << "Handler2 is handle request: " << req.getDescription() << endl;
    56. }
    57. };
    58. class Handler3 : public ChainHandler {
    59. protected:
    60. virtual bool canHandleRequest(const Request& req) override {
    61. return req.getReqType() == RequestType::REQ_HANDLER3;
    62. }
    63. virtual void processRequest(const Request& req) override {
    64. cout << "Handler3 is handle request: " << req.getDescription() << endl;
    65. }
    66. };
    67. int main() {
    68. Handler1 h1;
    69. Handler1 h2;
    70. Handler1 h3;
    71. h1.setNextChain(&h2);
    72. h2.setNextChain(&h3);
    73. Request req("process task ...", RequestType::REQ_HANDLER3);
    74. h1.handle(req);
    75. return 0;
    76. }

    要点总结:

    Chain of Responsibility模式的应用场合在于“一个请求可能有多个接受者,但是最后真正的接受者只有一个“,这时候请求发送者与接受者的耦合有可能出现”变化脆弱“的症状,指责链的目的就是将两者解耦,从而更好地应对变化。

    应用了Chain of Responsibility模式后,对象的职责分派将更具灵活性。我们可以在运行时动态增加/修改请求的处理职责。

    如果请求传递到职责链的末尾乃得不到处理,应该有一个合理的缺省机制。这也使每一个接受对象的职责,而不是发出请求的对象的职责。

  • 相关阅读:
    Linux系统部署PostgreSQL 单机数据库
    C/C++数据结构——公路村村通(Prim)
    Towards a Rigorous Evaluation of Time-series Anomaly Detection(论文翻译)
    鸿鹄工程项目管理系统 Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统
    黑客技术(网络安全)学习
    多假设跟踪算法
    DevOps流程的简单总结
    结构方程模型调整
    NOIP2023模拟15联测36 均分财产
    Python趣味算法入门 - 打鱼还是晒网
  • 原文地址:https://blog.csdn.net/zhaodongdong2012/article/details/134519675