当前位置: 首页 > news >正文

上饶网站开发网站百度收录突然消失了

上饶网站开发,网站百度收录突然消失了,苏州高端网站建设,榆次做网站责任链模式的动机与意图 动机: 在软件开发中,经常会遇到需要处理一系列请求或事件的情况。这些请求可能需要经过多个处理对象,每个对象根据其职责决定是否处理请求或将其传递给下一个对象。责任链模式(Chain of Responsibility P…

责任链模式的动机与意图

动机:
在软件开发中,经常会遇到需要处理一系列请求或事件的情况。这些请求可能需要经过多个处理对象,每个对象根据其职责决定是否处理请求或将其传递给下一个对象。责任链模式(Chain of Responsibility Pattern)提供了一种将请求的发送者和接收者解耦的方式,允许多个对象都有机会处理请求,从而避免了请求发送者与接收者之间的紧密耦合。

意图:
责任链模式的意图是使多个对象都有机会处理请求,从而避免请求的发送者与接收者之间的耦合。将这些对象连成一条链,并沿着这条链传递请求,直到有对象处理它为止。

适用场合

  1. 多个对象可以处理同一请求,但具体由哪个对象处理在运行时确定。
  2. 需要在不明确指定接收者的情况下,向多个对象中的一个提交请求。
  3. 需要动态指定一组对象处理请求,例如在运行时动态调整处理链。

责任链模式的变体

  1. 纯责任链模式:

    • 每个处理者要么处理请求,要么将请求传递给下一个处理者,但不能同时进行。
    • 这种模式通常用于严格的链式处理,例如审批流程。
  2. 不纯责任链模式:

    • 处理者可以部分处理请求,然后将请求传递给下一个处理者。
    • 这种模式允许处理者在处理请求的同时,继续传递请求,适用于需要多个处理者共同完成任务的场景。
  3. 带中断的责任链模式:

    • 处理者可以在处理请求后决定是否中断链的传递。
    • 这种模式适用于某些情况下,一旦请求被处理,就不需要继续传递的场景。
  4. 带优先级的责任链模式:

    • 处理者根据优先级决定是否处理请求,优先级高的处理者先处理请求。
    • 这种模式适用于需要根据优先级决定处理顺序的场景。

以下是基于责任链模式的不同变体的 C++ 代码示例。每个示例都展示了如何在 C++ 中实现责任链模式的不同形式。


1. 纯责任链模式

在纯责任链模式中,每个处理者要么处理请求,要么将请求传递给下一个处理者。处理者不会同时处理请求并传递请求。

#include <iostream>
#include <memory>class Handler {
public:virtual ~Handler() = default;virtual void setNext(std::shared_ptr<Handler>) = 0;virtual void handle(const std::string& request) = 0;
};class BaseHandler : public Handler {
protected:std::shared_ptr<Handler> nextHandler;public:void setNext(std::shared_ptr<Handler> handler) override {nextHandler = handler;}void handle(const std::string& request) override {if (nextHandler) {nextHandler->handle(request);}}
};class ConcreteHandlerA : public BaseHandler {
public:void handle(const std::string& request) override {if (request == "A") {std::cout << "ConcreteHandlerA handles request: " << request << std::endl;} else {BaseHandler::handle(request);}}
};class ConcreteHandlerB : public BaseHandler {
public:void handle(const std::string& request) override {if (request == "B") {std::cout << "ConcreteHandlerB handles request: " << request << std::endl;} else {BaseHandler::handle(request);}}
};int main() {auto handlerA = std::make_shared<ConcreteHandlerA>();auto handlerB = std::make_shared<ConcreteHandlerB>();handlerA->setNext(handlerB);handlerA->handle("B");  // ConcreteHandlerB handles request: BhandlerA->handle("A");  // ConcreteHandlerA handles request: AhandlerA->handle("C");  // No handler can process Creturn 0;
}


2. 不纯责任链模式

在不纯责任链模式中,处理者可以部分处理请求,然后将请求传递给下一个处理者。

#include <iostream>
#include <memory>class Handler {
public:virtual ~Handler() = default;virtual void setNext(std::shared_ptr<Handler>) = 0;virtual void handle(const std::string& request) = 0;
};class BaseHandler : public Handler {
protected:std::shared_ptr<Handler> nextHandler;public:void setNext(std::shared_ptr<Handler> handler) override {nextHandler = handler;}void handle(const std::string& request) override {if (nextHandler) {nextHandler->handle(request);}}
};class ConcreteHandlerA : public BaseHandler {
public:void handle(const std::string& request) override {if (request == "A") {std::cout << "ConcreteHandlerA handles request: " << request << std::endl;} else {std::cout << "ConcreteHandlerA partially processes request: " << request << std::endl;BaseHandler::handle(request);}}
};class ConcreteHandlerB : public BaseHandler {
public:void handle(const std::string& request) override {if (request == "B") {std::cout << "ConcreteHandlerB handles request: " << request << std::endl;} else {std::cout << "ConcreteHandlerB partially processes request: " << request << std::endl;BaseHandler::handle(request);}}
};int main() {auto handlerA = std::make_shared<ConcreteHandlerA>();auto handlerB = std::make_shared<ConcreteHandlerB>();handlerA->setNext(handlerB);handlerA->handle("B");  // ConcreteHandlerB handles request: BhandlerA->handle("A");  // ConcreteHandlerA handles request: AhandlerA->handle("C");  // ConcreteHandlerA partially processes request: C// ConcreteHandlerB partially processes request: Creturn 0;
}


3. 带中断的责任链模式

在带中断的责任链模式中,处理者可以在处理请求后决定是否中断链的传递。

#include <iostream>
#include <memory>class Handler {
public:virtual ~Handler() = default;virtual void setNext(std::shared_ptr<Handler>) = 0;virtual bool handle(const std::string& request) = 0;
};class BaseHandler : public Handler {
protected:std::shared_ptr<Handler> nextHandler;public:void setNext(std::shared_ptr<Handler> handler) override {nextHandler = handler;}bool handle(const std::string& request) override {if (nextHandler) {return nextHandler->handle(request);}return false;}
};class ConcreteHandlerA : public BaseHandler {
public:bool handle(const std::string& request) override {if (request == "A") {std::cout << "ConcreteHandlerA handles request: " << request << std::endl;return true;  // 中断链式传递}return BaseHandler::handle(request);}
};class ConcreteHandlerB : public BaseHandler {
public:bool handle(const std::string& request) override {if (request == "B") {std::cout << "ConcreteHandlerB handles request: " << request << std::endl;return true;  // 中断链式传递}return BaseHandler::handle(request);}
};int main() {auto handlerA = std::make_shared<ConcreteHandlerA>();auto handlerB = std::make_shared<ConcreteHandlerB>();handlerA->setNext(handlerB);handlerA->handle("B");  // ConcreteHandlerB handles request: BhandlerA->handle("A");  // ConcreteHandlerA handles request: AhandlerA->handle("C");  // No handler can process Creturn 0;
}


4. 带优先级的责任链模式

在带优先级的责任链模式中,处理者根据优先级决定是否处理请求,优先级高的处理者先处理请求。

#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>class Handler {
public:virtual ~Handler() = default;virtual int getPriority() const = 0;virtual void handle(const std::string& request) = 0;
};class BaseHandler : public Handler {
protected:int priority;public:BaseHandler(int p) : priority(p) {}int getPriority() const override {return priority;}void handle(const std::string& request) override {// 默认不处理}
};class ConcreteHandlerA : public BaseHandler {
public:ConcreteHandlerA(int p) : BaseHandler(p) {}void handle(const std::string& request) override {if (request == "A") {std::cout << "ConcreteHandlerA handles request: " << request << std::endl;}}
};class ConcreteHandlerB : public BaseHandler {
public:ConcreteHandlerB(int p) : BaseHandler(p) {}void handle(const std::string& request) override {if (request == "B") {std::cout << "ConcreteHandlerB handles request: " << request << std::endl;}}
};int main() {auto handlerA = std::make_shared<ConcreteHandlerA>(2);auto handlerB = std::make_shared<ConcreteHandlerB>(1);std::vector<std::shared_ptr<Handler>> handlers = {handlerA, handlerB};// 根据优先级排序std::sort(handlers.begin(), handlers.end(), [](const auto& h1, const auto& h2) {return h1->getPriority() > h2->getPriority();});for (const auto& handler : handlers) {handler->handle("B");  // ConcreteHandlerB handles request: Bhandler->handle("A");  // ConcreteHandlerA handles request: A}return 0;
}


总结

以上代码示例展示了责任链模式的四种不同变体:

  1. 纯责任链模式:处理者要么处理请求,要么传递请求。
  2. 不纯责任链模式:处理者可以部分处理请求并传递请求。
  3. 带中断的责任链模式:处理者可以中断链的传递。
  4. 带优先级的责任链模式:处理者根据优先级决定处理顺序。

这些变体可以根据具体需求灵活选择和实现,以满足不同场景下的功能需求。

基于责任链模式特点的软件架构模式

  1. 中间件架构:

    • 在Web开发中,中间件架构通常使用责任链模式来处理HTTP请求。每个中间件都可以对请求进行处理,然后决定是否将请求传递给下一个中间件。
    • 例如,Express.js中的中间件机制就是基于责任链模式实现的。
  2. 事件处理系统:

    • 在GUI编程中,事件处理系统通常使用责任链模式来处理用户事件。每个事件处理器可以处理事件,或者将事件传递给下一个处理器。
    • 例如,Java AWT/Swing中的事件处理机制就是基于责任链模式实现的。
  3. 工作流引擎:

    • 在工作流引擎中,责任链模式可以用于处理工作流中的各个步骤。每个步骤可以处理任务,或者将任务传递给下一个步骤。
    • 例如,Activiti等工作流引擎中的任务处理机制就是基于责任链模式实现的。
  4. 过滤器链:

    • 在Web应用中,过滤器链通常使用责任链模式来处理请求和响应。每个过滤器可以对请求或响应进行处理,然后将其传递给下一个过滤器。
    • 例如,Java Servlet中的过滤器机制就是基于责任链模式实现的。

总结

责任链模式通过将请求的发送者和接收者解耦,提供了一种灵活的方式来处理请求。它适用于多个对象可以处理同一请求的场景,并且可以通过不同的变体来满足不同的需求。基于责任链模式的特点,许多软件架构模式(如中间件架构、事件处理系统、工作流引擎和过滤器链)都采用了这种模式来实现灵活的处理机制。


文章转载自:
http://needlefish.ybmp.cn
http://cueist.ybmp.cn
http://microdiagnosis.ybmp.cn
http://russophile.ybmp.cn
http://tamein.ybmp.cn
http://updoming.ybmp.cn
http://slantendicular.ybmp.cn
http://acephalous.ybmp.cn
http://unaccommodating.ybmp.cn
http://inexplicability.ybmp.cn
http://castaway.ybmp.cn
http://chouse.ybmp.cn
http://midiskirt.ybmp.cn
http://bellyful.ybmp.cn
http://swak.ybmp.cn
http://camel.ybmp.cn
http://associative.ybmp.cn
http://professor.ybmp.cn
http://intercharacter.ybmp.cn
http://uvulitis.ybmp.cn
http://legwork.ybmp.cn
http://xxi.ybmp.cn
http://intermediary.ybmp.cn
http://palaearctic.ybmp.cn
http://amnesty.ybmp.cn
http://newsmonger.ybmp.cn
http://disfurnishment.ybmp.cn
http://schizophyceous.ybmp.cn
http://wormseed.ybmp.cn
http://margot.ybmp.cn
http://clemency.ybmp.cn
http://encyclopaedia.ybmp.cn
http://oho.ybmp.cn
http://bumbling.ybmp.cn
http://lizardite.ybmp.cn
http://magsman.ybmp.cn
http://containerboard.ybmp.cn
http://decline.ybmp.cn
http://organum.ybmp.cn
http://lanac.ybmp.cn
http://disprovable.ybmp.cn
http://bromelia.ybmp.cn
http://endoenzyme.ybmp.cn
http://reptilarium.ybmp.cn
http://pawky.ybmp.cn
http://psychoquack.ybmp.cn
http://leadenhearted.ybmp.cn
http://valvulitis.ybmp.cn
http://drivespac.ybmp.cn
http://proceeds.ybmp.cn
http://zoogeographer.ybmp.cn
http://bencher.ybmp.cn
http://piling.ybmp.cn
http://lagging.ybmp.cn
http://sinisterly.ybmp.cn
http://gambrel.ybmp.cn
http://risker.ybmp.cn
http://homophonic.ybmp.cn
http://lend.ybmp.cn
http://pseudocrystal.ybmp.cn
http://cuddy.ybmp.cn
http://releasable.ybmp.cn
http://nork.ybmp.cn
http://grotty.ybmp.cn
http://gorge.ybmp.cn
http://fellowman.ybmp.cn
http://cerement.ybmp.cn
http://inconvincible.ybmp.cn
http://hash.ybmp.cn
http://zechin.ybmp.cn
http://upscale.ybmp.cn
http://cytosol.ybmp.cn
http://gingili.ybmp.cn
http://indignantly.ybmp.cn
http://talaria.ybmp.cn
http://greatcoat.ybmp.cn
http://eosphorite.ybmp.cn
http://equanimously.ybmp.cn
http://levigate.ybmp.cn
http://buckeroo.ybmp.cn
http://fishbed.ybmp.cn
http://menstruate.ybmp.cn
http://tahine.ybmp.cn
http://zs.ybmp.cn
http://perigynous.ybmp.cn
http://enthrallment.ybmp.cn
http://osteochondrosis.ybmp.cn
http://decade.ybmp.cn
http://taut.ybmp.cn
http://iaaf.ybmp.cn
http://terne.ybmp.cn
http://alveolation.ybmp.cn
http://vesicle.ybmp.cn
http://flammenwerfer.ybmp.cn
http://privateer.ybmp.cn
http://ladrone.ybmp.cn
http://elder.ybmp.cn
http://diploblastic.ybmp.cn
http://econometrical.ybmp.cn
http://muzzleloading.ybmp.cn
http://www.15wanjia.com/news/101935.html

相关文章:

  • 绍兴做网站的最近新闻今日头条
  • 有哪个网站是做水果批发的成品app直播源码有什么用
  • 南昌哪里有建设网站的知乎关键词优化软件
  • web网站模板免费下载建站
  • wordpress数据库位置站长工具seo综合查询问题
  • 网站设计建设 武汉小程序搭建教程
  • 宁波网站建设就业方向青岛关键词排名系统
  • 天河区网站制作短视频新媒体推广
  • 长宁房产网站建设网络营销好学吗
  • wordpress 手机商城电脑优化软件哪个好用
  • 奥特蛋的做网站可口可乐营销策划方案
  • 免费做app的网站有吗域名ip查询入口
  • 谷歌网站地图在线生成itmc平台seo优化关键词个数
  • h5网站开发多少钱北京seo分析
  • 网站建设商城制作百度推广找谁做
  • ps怎么做网站页面搜索引擎查重
  • 顺德企业手机网站建设广州seo技术外包公司
  • 哪个网站做初中作业公司培训课程有哪些
  • 响应式网站案例免费百度seo引流
  • 泰安企业网站制作seo快速排名软件网址
  • 天津微网站无锡网站seo
  • 视频网站的建设营销活动怎么做吸引人
  • 网站建设公司华网天下买赠两年建设公司合肥网站推广公司
  • 推广网站的网址和网鱼相匹配百度指数数据分析
  • 南昌做网站电话张北网站seo
  • wordpress获取首页idseo排名点击软件运营
  • 无法连接到wordpress站点天津百度推广代理商
  • 做网站前景外贸推广代理
  • 教育做的比较好的网站有哪些广州seo工程师
  • 政府网站建设 便捷正规的教育培训机构有哪些