图解设计模式
来源:互联网 发布:mysql use index 编辑:程序博客网 时间:2024/06/05 14:40
读书笔记 仅供参考
简述
职责链模式将多个对象组成一条职责链,然后按照他们在职责链上的顺序一个个找出到底应该谁负责处理。也是推卸责任的结构。
角色和 UML
Handler
定义了处理请求的接口(API),该角色知道下一个处理者是谁,如果自己无法处理,将请求转发给下一个。
ConcreteHandler
处理请求的具体角色
Client
向第一个 ConcreteHandler 角色发送请求的角色。
UML
例子
例程是一个不同人解决问题的程序,问题具有编号,而每一种处理对象都只能解决特定编号的问题,比如:都不能解决、解决小于特定数的、解决等于特定数的和解决奇数的。
//问题类,具有问题编号,编号是职责链处理的依据public class Trouble { private int number; public Trouble(int number) { this.number = number; } public int getNumber() { return number; } @Override public String toString() { return "[Trouble " + number + "]"; }}
//解决问题德抽象类public abstract class Support { //实例的名字 private String name; //下一个处理人员 private Support next; public Support(String name) { this.name = name; } public Support setNext(Support next) { this.next = next; return next; } public final void support(Trouble trouble) { if (resolve(trouble)) { done(trouble); } else if (next != null) { next.support(trouble); } else { fail(trouble); } } public String toString() { return "[" + name + "]"; } //解决问题的办法 protected abstract boolean resolve(Trouble trouble); //解决 protected void done(Trouble trouble) { System.out.println(trouble + " is resolved by" + this + "."); } //未解决 protected void fail(Trouble trouble) { System.out.println(trouble + " cannot be resolved."); }}
//永远不解决问题public class NoSupport extends Support { public NoSupport(String name) { super(name); } @Override protected boolean resolve(Trouble trouble) { return false; }}
//只解决编号小于 limit 的问题public class LimitSupport extends Support { private int limit; public LimitSupport(String name, int limit) { super(name); this.limit = limit; } @Override protected boolean resolve(Trouble trouble) { if(trouble.getNumber() < limit) { return true; } else { return false; } }}
//只解决奇数编号问题public class OddSupport extends Support { public OddSupport(String name) { super(name); } @Override protected boolean resolve(Trouble trouble) { if (trouble.getNumber() % 2 == 1) { return true; } else { return false; } }}
//只解决指定编号的问题public class SpecialSupport extends Support { private int number; public SpecialSupport(String name, int number) { super(name); this.number = number; } @Override protected boolean resolve(Trouble trouble) { if (trouble.getNumber() == number) { return true; } else { return false; } }}
public class Main { public static void main(String[] args) { Support alice = new NoSupport("ALice"); Support bob = new LimitSupport("Bob", 100); Support charlie = new SpecialSupport("Charlie", 429); Support diana = new LimitSupport("Diana", 200); Support elmo = new OddSupport("Elem"); Support fred = new LimitSupport("Fred", 300); alice.setNext(bob).setNext(charlie).setNext(diana).setNext(elmo).setNext(fred); for (int i = 0; i < 500; i += 33) { alice.support(new Trouble(i)); } }}
运行结果
UML
要点
弱化了发出请求的人和处理请求的人之间的关系
发出请求的人并不知道谁应该处理请求。
可以动态地修改职责链
专注于自己的工作
推卸请求会导致一定的延迟
阅读全文
0 0
- 设计模式图解
- gof23设计模式图解
- 图解设计模式
- 图解基础设施设计模式
- 设计模式图解
- 图解设计模式
- 图解设计模式
- 图解设计模式
- 图解设计模式
- 图解设计模式
- 图解设计模式
- 图解设计模式
- 图解设计模式
- 图解设计模式
- 图解设计模式
- 图解设计模式
- 图解设计模式
- 图解设计模式
- wxPython Recipes.pdf 2017 英文原版 免费下载
- Codeforces 903D Almost Difference
- 文章标题
- ROS机器人程序设计(原书第二版)笔记
- Spring Boot 拦截器 请求参数MD5签名校验
- 图解设计模式
- string转化成int
- JavaScript 实用技巧和写法建议
- 每天一道LeetCode-----将链表中满足条件的节点移动到前面
- 系统负载能力浅析
- 515. Find Largest Value in Each Tree Row
- 成为 Linux 终端高手的七种武器
- 亲自动手写一个Python库(一)
- spring整合hibernate的时候使用session的时候报:is not valid without active transaction