多例责任链模式
在本案例中我们模拟在618大促期间的业务系统上线审批流程场景像是这些一线电商类的互联网公司阿里、京东、拼多多等在618期间都会做一些运营活动场景以及提供的扩容备战就像过年期间百度的红包一样。但是所有开发的这些系统都需要陆续的上线因为临近618有时候也有一些紧急的调整的需要上线但为了保障线上系统的稳定性是尽可能的减少上线的也会相应的增强审批力度。就像一级响应、二级响应一样。而这审批的过程在随着特定时间点会增加不同级别的负责人加入每个人就像责任链模式中的每一个核心点。对于研发小伙伴并不需要关心具体的审批流程处理细节只需要知道这个上线更严格级别也更高但对于研发人员来说同样是点击相同的提审按钮等待审核。接下来我们就模拟这样一个业务诉求场景使用责任链的设计模式来实现此功能。组成部分在多例责任链模式中可以精简地概括为三个方面组成分别是自定义的链表结构策略类实现策略链表化的类以及装配此类的装配链路类。首先定义的是顶层的链接口public interface ILinkE { boolean add(E e); boolean addFirst(E e); boolean addLast(E e); boolean remove(Object o); E get(int index); void printLinkList(); }然后是实现接口的自定义链表类public class LinkedListE implements ILinkE { /** * 责任链名称 */ private final String name; transient int size 0; transient NodeE first; transient NodeE last; public LinkedList(String name) { this.name name; } void linkFirst(E e) { final NodeE f first; final NodeE newNode new Node(null, e, f); first newNode; if (f null) last newNode; else f.prev newNode; size; } void linkLast(E e) { final NodeE l last; final NodeE newNode new Node(l, e, null); last newNode; if (l null) { first newNode; } else { l.next newNode; } size; } Override public boolean add(E e) { linkLast(e); return true; } Override public boolean addFirst(E e) { linkFirst(e); return true; } Override public boolean addLast(E e) { linkLast(e); return true; } Override public boolean remove(Object o) { if (o null) { for (NodeE x first; x ! null; x x.next) { if (x.item null) { unlink(x); return true; } } } else { for (NodeE x first; x ! null; x x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } E unlink(NodeE x) { final E element x.item; final NodeE next x.next; final NodeE prev x.prev; if (prev null) { first next; } else { prev.next next; x.prev null; } if (next null) { last prev; } else { next.prev prev; x.next null; } x.item null; size--; return element; } Override public E get(int index) { return node(index).item; } NodeE node(int index) { if (index (size 1)) { NodeE x first; for (int i 0; i index; i) x x.next; return x; } else { NodeE x last; for (int i size - 1; i index; i--) x x.prev; return x; } } public void printLinkList() { if (this.size 0) { System.out.println(链表为空); } else { NodeE temp first; System.out.print(目前的列表头节点 first.item 尾节点 last.item 整体); while (temp ! null) { System.out.print(temp.item ); temp temp.next; } System.out.println(); } } protected static class NodeE { E item; NodeE next; NodeE prev; public Node(NodeE prev, E element, NodeE next) { this.item element; this.next next; this.prev prev; } } public String getName() { return name; } }以上是我们的功能部分实现下面是策略类package cn.bugstack.types.design.framework.link.model2.handler; public interface ILogicHandlerT, D, R { default R next(T requestParameter, D dynamicContext) { return null; } R apply(T requestParameter, D dynamicContext) throws Exception; }下面是我们的主角真正实现策略链表化并且实现逻辑的业务类public class BusinessLinkedListT, D, R extends LinkedListILogicHandlerT, D, R implements ILogicHandlerT, D, R{ public BusinessLinkedList(String name) { super(name); } Override public R apply(T requestParameter, D dynamicContext) throws Exception { NodeILogicHandlerT, D, R current this.first; do{ ILogicHandlerT, D, R item current.item; R apply item.apply(requestParameter, dynamicContext); if(apply ! null) return apply; current current.next; }while(current ! null); return null; } }最后还有一个策略装配类它封装了BusinessLinkedList作用是初始化以及get。public class LinkArmoryT, D, R { private final BusinessLinkedListT, D, R logicLink; SafeVarargs public LinkArmory(String linkName, ILogicHandlerT, D, R... logicHandlers) { logicLink new BusinessLinkedList(linkName); for (ILogicHandlerT, D, R logicHandler: logicHandlers){ logicLink.add(logicHandler); } } public BusinessLinkedListT, D, R getLogicLink() { return logicLink; } }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2422225.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!