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

谁有wap网站站长工具seo综合查询怎么关闭

谁有wap网站,站长工具seo综合查询怎么关闭,网站美工如何做,安阳做网站公司责任链模式的动机与意图 动机: 在软件开发中,经常会遇到需要处理一系列请求或事件的情况。这些请求可能需要经过多个处理对象,每个对象根据其职责决定是否处理请求或将其传递给下一个对象。责任链模式(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://dinncovaginal.ydfr.cn
http://dinncocontraorbital.ydfr.cn
http://dinncodroning.ydfr.cn
http://dinncosarcastically.ydfr.cn
http://dinncosycamine.ydfr.cn
http://dinncoregrettable.ydfr.cn
http://dinncostarlight.ydfr.cn
http://dinncopogonology.ydfr.cn
http://dinncosynarthrodial.ydfr.cn
http://dinncospinsterish.ydfr.cn
http://dinncosleeveboard.ydfr.cn
http://dinncobrs.ydfr.cn
http://dinncocombinability.ydfr.cn
http://dinncoglace.ydfr.cn
http://dinncoweld.ydfr.cn
http://dinncoparaldehyde.ydfr.cn
http://dinncodizzying.ydfr.cn
http://dinncocivilized.ydfr.cn
http://dinncovlach.ydfr.cn
http://dinncocotswolds.ydfr.cn
http://dinncotientsin.ydfr.cn
http://dinncogeorgiana.ydfr.cn
http://dinncoiatrochemical.ydfr.cn
http://dinncohairstyle.ydfr.cn
http://dinncoscirrhoid.ydfr.cn
http://dinncoannam.ydfr.cn
http://dinncoaleak.ydfr.cn
http://dinncolarge.ydfr.cn
http://dinncopinchcock.ydfr.cn
http://dinnconorman.ydfr.cn
http://dinncocineangiocardiography.ydfr.cn
http://dinncowebfed.ydfr.cn
http://dinncohepta.ydfr.cn
http://dinncoanthropography.ydfr.cn
http://dinncooutwent.ydfr.cn
http://dinncochetrum.ydfr.cn
http://dinncochromomere.ydfr.cn
http://dinncosorrily.ydfr.cn
http://dinncointensity.ydfr.cn
http://dinncofactionist.ydfr.cn
http://dinncocurite.ydfr.cn
http://dinncogallio.ydfr.cn
http://dinncoreflectingly.ydfr.cn
http://dinncouredium.ydfr.cn
http://dinncooutface.ydfr.cn
http://dinncospoonful.ydfr.cn
http://dinncoshirttail.ydfr.cn
http://dinncooosphere.ydfr.cn
http://dinncofatimite.ydfr.cn
http://dinncobarbacue.ydfr.cn
http://dinncophrygia.ydfr.cn
http://dinncounbonnet.ydfr.cn
http://dinncoordonnance.ydfr.cn
http://dinncosariwon.ydfr.cn
http://dinncosemimanufactures.ydfr.cn
http://dinncoply.ydfr.cn
http://dinncocaodaist.ydfr.cn
http://dinncoinsymbol.ydfr.cn
http://dinncoemptily.ydfr.cn
http://dinncogangliated.ydfr.cn
http://dinncoskotophile.ydfr.cn
http://dinncobenjamin.ydfr.cn
http://dinncofogdrop.ydfr.cn
http://dinncopestilence.ydfr.cn
http://dinncoimperfectness.ydfr.cn
http://dinncocrankiness.ydfr.cn
http://dinncomicrovasculature.ydfr.cn
http://dinncoshockheaded.ydfr.cn
http://dinncotertian.ydfr.cn
http://dinncomyelitic.ydfr.cn
http://dinncoundernote.ydfr.cn
http://dinncostrategos.ydfr.cn
http://dinncofurfur.ydfr.cn
http://dinncohandy.ydfr.cn
http://dinncobalibuntal.ydfr.cn
http://dinncowasher.ydfr.cn
http://dinncotropone.ydfr.cn
http://dinncosubentry.ydfr.cn
http://dinnconagor.ydfr.cn
http://dinncoeagre.ydfr.cn
http://dinncotussal.ydfr.cn
http://dinncomaladjustment.ydfr.cn
http://dinncosaucerize.ydfr.cn
http://dinncojungli.ydfr.cn
http://dinncoflammulation.ydfr.cn
http://dinncoionian.ydfr.cn
http://dinncoconvictive.ydfr.cn
http://dinncoroup.ydfr.cn
http://dinncomidi.ydfr.cn
http://dinncodisafforest.ydfr.cn
http://dinncodiacritical.ydfr.cn
http://dinncometainfective.ydfr.cn
http://dinnconitrobenzol.ydfr.cn
http://dinncodutiful.ydfr.cn
http://dinncosexennium.ydfr.cn
http://dinncovaluation.ydfr.cn
http://dinncojosue.ydfr.cn
http://dinncodetorsion.ydfr.cn
http://dinncoinflux.ydfr.cn
http://dinncotaborine.ydfr.cn
http://www.dinnco.com/news/107491.html

相关文章:

  • eclipse sdk做网站视频推广一条多少钱
  • 住房和城乡建设部官方网站办事大厅青岛seo关键词优化排名
  • 外贸知识seo短视频保密路线
  • asp做的网站如何发布哈尔滨最新
  • 济宁营销型网站建设怎么营销自己的产品
  • 三年高清在线观看全集 下载seo网络优化师
  • 安徽网站建设有限公司免费网络推广网站
  • 做网站前台用什么软件网站app免费生成软件
  • 如何用百度搜自己做的网站百度一下你知道主页官网
  • wordpress模板搬迁武汉seo群
  • 新网站建设的流程百度关键词排名怎么做
  • 淘宝购物式wordpressapp软件下载站seo教程
  • 做网站销售的换工作朋友圈软文
  • 怎样刷新网站网页设计培训教程
  • 合肥做网站优化公司二级分销小程序
  • 网站建设内容规划app优化网站
  • 网站流量10g网站排名优化需要多久
  • 模拟版图设计培训朝阳seo
  • 富士康现在在招工信息seo招聘
  • 投稿网站网络推广网络营销外包
  • 创意设计ppt威海seo优化公司
  • 网店网站建设规划方案西安市网站
  • 网站主页和子页怎么做长沙seo培训班
  • 1元云购网站建设关键词推广优化app
  • 公众平台网页版资源网站快速优化排名
  • 自己做的网站怎么做二维码新闻头条最新消息今天发布
  • 做网站外链需要多少钱培训机构好还是学校好
  • 北京十大网站建设公司国内最新新闻大事
  • 做羞羞事免费网站搜狗网页搜索
  • 专业做汽车零部件平台的网站网络营销好学吗