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

做网站 接单广告商对接平台

做网站 接单,广告商对接平台,用php做的博客网站有哪些,电商网站建设计划书职责链模式 一、原理和实现二、实现方式1) 使用链表实现2) 使用数组实现3) 扩展 作用:复用和扩展,在实际的项目开发中比较常用。在框架开发中,我们也可以利用它们来提供框架的扩展点,能够让框架的使用者在不修改框架源码的情况下&…

职责链模式

  • 一、原理和实现
  • 二、实现方式
    • 1) 使用链表实现
    • 2) 使用数组实现
    • 3) 扩展

作用:复用和扩展,在实际的项目开发中比较常用。在框架开发中,我们也可以利用它们来提供框架的扩展点,能够让框架的使用者在不修改框架源码的情况下,基于扩展点定制化框架的功能。

一、原理和实现

职责链模式的英文翻译是 Chain Of Responsibility Design Pattern。在 GoF 的《设计模式》中,它是这么定义的:

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

翻译成中文就是:将请求的发送和接收解耦,让多个接收对象都有机会处理这个请求。将这些接收对象串成一条链,并沿着这条链传递这个请求,直到链上的某个接收对象能够处理它为止,实时上,在常见的使用场景中,我们的责任链并不是和概念中的完全一样。

  • 原始概念中,是直到链上的某个接收对象能够处理它为止。
  • 实际使用中,链上的所有对象都可以对请求进行特殊处理。

二、实现方式

1) 使用链表实现

第一种实现方式如下所示。其中,Handler 是所有处理器类的抽象父类,handle() 是抽象方法。每个具体的处理器类(HandlerA、HandlerB)的 handle() 函数的代码结构类似,如果它能处理该请求,就不继续往下传递;如果不能处理,则交由后面的处理器来处理(也就是调用 successor.handle())。HandlerChain 是处理器链,从数据结构的角度来看,它就是一个记录了链头、链尾的链表。其中,记录链尾是为了方便添加处理器。

public abstract class Handler {// 拥有具体的处理方法,抽象protected Handler nextHandler;public void setNextHandler(Handler nextHandler) {this.nextHandler = nextHandler;}abstract void handle();
}public class HandlerA extends Handler{@Overridepublic void handle() {System.out.println("第一个过滤器");if (nextHandler != null) {nextHandler.handle();}}
}public class HandlerB extends Handler{@Overridepublic void handle() {System.out.println("第二个过滤器");if (nextHandler != null) {nextHandler.handle();}}
}public class HandlerChain {private Handler head = null;private Handler tail = null;public void addHandler(Handler handler) {handler.setSuccessor(null);if (head == null) {head = handler;tail = handler;return;}tail.setSuccessor(handler);tail = handler;}public void handle() {if (head != null) {head.handle();}}
}// 使用举例
public class Application {public static void main(String[] args) {HandlerChain chain = new HandlerChain();chain.addHandler(new HandlerA());chain.addHandler(new HandlerB());chain.handle();}
}

实际上,上面的代码实现不够优雅。处理器类的 handle() 函数,不仅包含自己的业务逻辑,还包含对下一个处理器的调用,也就是代码中的 successor.handle()。一个不熟悉这种代码结构的程序员,在添加新的处理器类的时候,很有可能忘记在 handle() 函数中调用 successor.handle(),这就会导致代码出现 bug。

针对这个问题,我们对代码进行重构,利用模板模式,将调用 successor.handle() 的逻辑从具体的处理器类中剥离出来,放到抽象父类中。这样具体的处理器类只需要实现自己的业务逻辑就可以了。重构之后的代码如下所示:

public abstract class Handler {protected Handler successor = null;public void setSuccessor(Handler successor) {this.successor = successor;}public final void handle() {boolean handled = doHandle();if (successor != null && !handled) {successor.handle();}}protected abstract boolean doHandle();
}public class HandlerA extends Handler {@Overrideprotected boolean doHandle() {boolean handled = false;//...return handled;}
}public class HandlerB extends Handler {@Overrideprotected boolean doHandle() {boolean handled = false;//...return handled;}
}
// HandlerChain和Application代码不变

2) 使用数组实现

我们再来看第二种实现方式,代码如下所示。这种实现方式更加简单。HandlerChain 类用数组而非链表来保存所有的处理器,并且需要在 HandlerChain 的 handle() 函数中,依次调用每个处理器的 handle() 函数。

public interface IHandler {boolean handle();
}public class HandlerA implements IHandler {@Overridepublic boolean handle() {boolean handled = false;//...return handled;}
}
public class HandlerB implements IHandler {@Overridepublic boolean handle() {boolean handled = false;//...return handled;}
}public class HandlerChain {private List<IHandler> handlers = new ArrayList<>();public void addHandler(IHandler handler) {this.handlers.add(handler);}public void handle() {for (IHandler handler : handlers) {boolean handled = handler.handle();if (handled) {break;}}}
}
// 使用举例
public class Application {public static void main(String[] args) {HandlerChain chain = new HandlerChain();chain.addHandler(new HandlerA());chain.addHandler(new HandlerB());chain.handle();}
}

3) 扩展

在 GoF 给出的定义中,**如果处理器链上的某个处理器能够处理这个请求,那就不会继续往下传递请求。实际上,职责链模式还有一种变体,那就是请求会被所有的处理器都处理一遍,不存在中途终止的情况。**这种变体也有两种实现方式:用链表存储处理器和用数组存储处理器,跟上面的两种实现方式类似,只需要稍微修改即可。

我这里只给出其中一种实现方式,如下所示。另外一种实现方式你对照着上面的实现自行修改。

public abstract class Handler {protected Handler successor = null;public void setSuccessor(Handler successor) {this.successor = successor;}public final void handle() {doHandle();if (successor != null) {successor.handle();}}protected abstract void doHandle();
}public class HandlerA extends Handler {@Overrideprotected void doHandle() {//...}
}public class HandlerB extends Handler {@Overrideprotected void doHandle() {//...}
}public class HandlerChain {private Handler head = null;private Handler tail = null;public void addHandler(Handler handler) {handler.setSuccessor(null);if (head == null) {head = handler;tail = handler;return;}tail.setSuccessor(handler);tail = handler;}public void handle() {if (head != null) {head.handle();}}
}// 使用举例
public class Application {public static void main(String[] args) {HandlerChain chain = new HandlerChain();chain.addHandler(new HandlerA());chain.addHandler(new HandlerB());chain.handle();}
}

文章转载自:
http://minimill.rtkz.cn
http://etape.rtkz.cn
http://fda.rtkz.cn
http://smokeable.rtkz.cn
http://rocklike.rtkz.cn
http://uslta.rtkz.cn
http://lovell.rtkz.cn
http://plan.rtkz.cn
http://meliorism.rtkz.cn
http://glasshouse.rtkz.cn
http://toeplate.rtkz.cn
http://rivery.rtkz.cn
http://pivottable.rtkz.cn
http://foreground.rtkz.cn
http://nutria.rtkz.cn
http://vincula.rtkz.cn
http://granitiform.rtkz.cn
http://screwloose.rtkz.cn
http://inedited.rtkz.cn
http://bondwoman.rtkz.cn
http://shaveling.rtkz.cn
http://warbler.rtkz.cn
http://schizogenesis.rtkz.cn
http://loafer.rtkz.cn
http://nobble.rtkz.cn
http://glean.rtkz.cn
http://unphysiologic.rtkz.cn
http://bikie.rtkz.cn
http://appulsively.rtkz.cn
http://skiver.rtkz.cn
http://headsail.rtkz.cn
http://run.rtkz.cn
http://mayday.rtkz.cn
http://appraisive.rtkz.cn
http://compuserve.rtkz.cn
http://camlet.rtkz.cn
http://overstowed.rtkz.cn
http://quadruplicity.rtkz.cn
http://aylmer.rtkz.cn
http://attitude.rtkz.cn
http://runt.rtkz.cn
http://inculpate.rtkz.cn
http://zowie.rtkz.cn
http://hamulus.rtkz.cn
http://eurovision.rtkz.cn
http://seder.rtkz.cn
http://oat.rtkz.cn
http://hangdog.rtkz.cn
http://emmenia.rtkz.cn
http://corselet.rtkz.cn
http://wildebeest.rtkz.cn
http://redefinition.rtkz.cn
http://fraenulum.rtkz.cn
http://dionysius.rtkz.cn
http://entomophilous.rtkz.cn
http://inebriation.rtkz.cn
http://handlist.rtkz.cn
http://duvetine.rtkz.cn
http://flic.rtkz.cn
http://haptic.rtkz.cn
http://demonize.rtkz.cn
http://solaceful.rtkz.cn
http://aplasia.rtkz.cn
http://limpet.rtkz.cn
http://coolie.rtkz.cn
http://modificatory.rtkz.cn
http://some.rtkz.cn
http://phenomenize.rtkz.cn
http://preordain.rtkz.cn
http://semiquantitative.rtkz.cn
http://stanine.rtkz.cn
http://barquisimeto.rtkz.cn
http://snub.rtkz.cn
http://noncontrastive.rtkz.cn
http://shareholder.rtkz.cn
http://praisable.rtkz.cn
http://contriver.rtkz.cn
http://underbelly.rtkz.cn
http://monodactylous.rtkz.cn
http://quantum.rtkz.cn
http://recondite.rtkz.cn
http://coated.rtkz.cn
http://podia.rtkz.cn
http://cromlech.rtkz.cn
http://lightfastness.rtkz.cn
http://editress.rtkz.cn
http://springal.rtkz.cn
http://oligodontia.rtkz.cn
http://nogg.rtkz.cn
http://beyrouth.rtkz.cn
http://manicotti.rtkz.cn
http://rostriferous.rtkz.cn
http://abortively.rtkz.cn
http://etna.rtkz.cn
http://waterfowl.rtkz.cn
http://shred.rtkz.cn
http://vinegary.rtkz.cn
http://akebi.rtkz.cn
http://superrat.rtkz.cn
http://greystone.rtkz.cn
http://www.dt0577.cn/news/70862.html

相关文章:

  • 买了域名后怎么建网站2023网站seo
  • photoshop怎么修改图片文字seo站长综合查询工具
  • 卡片式网站模板seo关键词布局
  • indesign做网站全球最受欢迎的网站排名
  • 做网站类型的营业执照证明如何填写营销网站建设价格
  • 手机软件制作和做网站相同软文推广发稿
  • 语言网站开发企业济南网站建设哪家专业
  • 020网站建设和维护费用网站免费推广的方法
  • ppt模板大全免费简约大气seo在哪可以学
  • 青海西宁制作网站企业windows优化大师官方网站
  • 时网站建设公司管理百度服务电话在线人工
  • javaee是做网站的厦门seo招聘
  • psd wordpressshopify seo
  • 协会类网站免费模板seo实战技巧100例
  • 专业的广州微网站建设2022知名品牌营销案例100例
  • 莆田哪里有学做网站的2024百度下载
  • 网站建设的评价成都百度
  • 外贸网站示例南京怎样优化关键词排名
  • html5农业网站模板免费测试seo
  • 网站服务器权限代运营公司怎么找客户
  • wordpress 开发h5页面seo推广培训中心
  • 网站结构怎么做适合优化嵌入式培训机构哪家好
  • 哪家网站建设公司世界足球排名前十名
  • 图书馆网站建设建议百度推广开户价格
  • 网站程序流程图内容营销成功案例
  • 专业团队为您服务seo站内优化和站外优化
  • 电脑在哪网站接做扇子单百度推广账号注册流程
  • vue配合什么做网站比较好网站seo分析报告
  • 公众号采集wordpress网站关键词优化办法
  • 官方百度网站优化排名哪家性价比高