刚学习了责任链模式,感觉还是蛮不错的,随手记录了下学习的内容。
责任链模式:责任链模式用于弱化请求发生者和请求处理者之间的关系。当多个对象都可以对请求进行处理,但不同的对象能处理的请求类型不同时,可以通过指向另一个对象的引用把这些对象连成一条责任链。当 Client 发出一个请求时,并不知道具体由哪个对象进行处理,它看到的只是一条责任链,将请求直接交给责任链,请求会在责任链中传递,直到找到一个能够进行处理的对象或者遍历结束找不到一个能够处理的对象为止。Java 语言中的异常处理机制就是责任链模式的一个典型应用例子。
下面模拟的是一个员工处理问题层次的责任链模式,不同级别的员工能处理不同级别的请求。
首先设计一个请求类:
package com.design.test.mode.responsibilityChain;
/**
* 请求操作
* 请求分级别,不同的级别需要不同层次的去处理一个请求
* @author Share
*
*/
public class Request {
public static final int TYPE_1 = 1;
public static final int TYPE_2 = 2;
public static final int TYPE_3 = 3;
public static final int TYPE_4 = 4;
private int type;
private String msg;
public Request(int type,String msg){
this.type=type;
this.msg=msg;
}
public int getType() {
return type;
}
public String getMsg() {
return msg;
}
}
设计员工的抽象类:
package com.design.test.mode.responsibilityChain;
/**
* 定义一个员工抽象类
* 一个员工有自己的上层,不同上层有处理问题的级别
* 如果最上层都处理不了这问题,则无人能处理这问题。
* 具体的员工类必须重写processRequest方法,才能见效
* @author Share
*
*/
public abstract class Employee {
protected Employee boss;
protected String name;
protected int requestLevel;
public Employee(Employee boss,String name){
this.boss = boss;
this.name = name;
}
public void processRequest(Request request){
if(boss!=null){
boss.processRequest(request);
}else{
System.out.println("Nobody can handle the request."+request.getMsg());
}
}
}
管理员员工类:
package com.design.test.mode.responsibilityChain;
public class Manager extends Employee {
public Manager(Employee boss, String name) {
super(boss, name);
requestLevel = Request.TYPE_1;
}
@Override
public void processRequest(Request request) {
if(request.getType() > requestLevel){
System.out.println(this.name+" say: I can't handle the request. My boss will handle it.");
super.processRequest(request);
}else{
System.out.println(this.name+" say: I can handle the request."+request.getMsg());
}
}
}
主管类:
package com.design.test.mode.responsibilityChain;
public class Director extends Employee {
public Director(Employee boss, String name) {
super(boss, name);
requestLevel = Request.TYPE_2;
}
@Override
public void processRequest(Request request) {
if(request.getType() > requestLevel){
System.out.println(this.name+" say: I can't handle the request. My boss will handle it.");
super.processRequest(request);
}else{
System.out.println(this.name+" say: I can handle the request."+request.getMsg());
}
}
}
CEO类:
package com.design.test.mode.responsibilityChain;
public class CEO extends Employee {
public CEO(Employee boss, String name) {
super(boss, name);
requestLevel = Request.TYPE_3;
}
@Override
public void processRequest(Request request) {
if(request.getType() > requestLevel){
System.out.println(this.name+" say: I can't handle the request. My boss will handle it.");
super.processRequest(request);
}else{
System.out.println(this.name+" say: I can handle the request."+request.getMsg());
}
}
}
测试类:
package com.design.test.mode.responsibilityChain;
public class Main {
public static void main(String[] args) {
CEO ceo = new CEO(null, "Jack Ceo");
Director dir = new Director(ceo, "Sello Director");
Manager manager = new Manager(dir, "Fewen Mananger");
Request req1 = new Request(4, "我要加薪");
Request req2 = new Request(2, "我要请假");
Request req3 = new Request(1, "我要加班");
System.out.println("处理请求1");
manager.processRequest(req1);
System.out.println("处理请求2");
manager.processRequest(req2);
System.out.println("处理请求3");
manager.processRequest(req3);
}
}
打印结果:
处理请求1
Fewen Mananger say: I can't handle the request. My boss will handle it.
Sello Director say: I can't handle the request. My boss will handle it.
Jack Ceo say: I can't handle the request. My boss will handle it.
Nobody can handle the request.我要加薪
处理请求2
Fewen Mananger say: I can't handle the request. My boss will handle it.
Sello Director say: I can handle the request.我要请假
处理请求3
Fewen Mananger say: I can handle the request.我要加班
分享到:
相关推荐
设计模式之责任链模式源码 设计模式之责任链模式源码 设计模式之责任链模式源码
设计模式之职责链模式,这份文档以例子的形式讲诉了设计模式之职责链模式,希望可以帮助需要的人!
设计模式之责任链模式Java版本实现和UML类图设计
java设计模式之责任链模式,从实际生活入手说明责任链模式的原理。
设计模式之职责链模式的代码详解,简单明了的将模式思想以代码编写出来,常用的设计模式
23种设计模式之责任链模式对责任链模式进行了简单的介绍,并附带了例子程序,和大家分享
一、责任链模式 现有 “战士”、“班长”、“排长”、“连长”,“营长”五种角色,当有人要请假时要进行以下处理 1.只能是下级象上级请假(如“排长”只能向“连长请假”) 2.班长可以批准1天内的假期,排长批5天,...
为解除请求的发送者和接收者之间耦合,而使多个对象都有机会处理这个请求。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它。
责任链模式(Chain) 用意:避免请求发送者与接受者耦合在一起,让多个对象连接成一条链,并且沿关这条链传递请求,直至有对象处理他为止。
职责链模式 设计模式 Chain of Responsibility 若有问题望指出。
本文实例讲述了Python设计模式之职责链模式原理与用法。分享给大家供大家参考,具体如下: 职责链模式(Chain Of Responsibility):使多个对象都有机会处理请求,从而避免发送者和接收者的耦合关系。将对象连成链并...
Java 23种设计模式17职责链模式.pdf
* @desc php设计模式之职责链模式(责任链模式) 定义:顾名思义,责任链模式为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。 在...
JAVA 设计模式: 工厂模式 代理模式 迭代模式 责任链模式 源码
【设计模式】之责任链模式责任链模式概念责任链,顾名思义,就是用来处理相关事务责任的一条执行链,执行链上有多个节点,每个节点都有机会(条件匹配)处理请求事务,如果
主要介绍了JAVA设计模式之责任链模式详解,需要的朋友可以参考下
设计模式的责任链模式的例子,希望对大家有用~~~~~~~~