核心模块:抽象处理者、具体处理者、客户端
所有代码请访问:git@code.aliyun.com:289804201/PatternLearn.git
使用场景:适用于权限不同的角色系统;
优点:发送者只需要将请求发送到链上,无需关心谁处理,达到发送者和处理者之间的解耦,允许动态的新增责任和删除责任;
缺点:请求可能不被处理或则循环,不能观察到内部状态
注意:
1,纯职责链模式要么承担全部责任,要么将责任推给下家;
2,不纯的职责链模式,android的按键分发流程;
3,与命令模式不同,命令模式多个不同的请求或者命令只有一个处理者;职责链模式多个对象可以接收同一个请求,直到有对象可以处理它为止(最后的处理者可以扔掉);
/**
* Created by tory on 2017/12/12.
* 要求:1,纯职责链模式要么承担全部责任,要么将责任推给下家
* 2,不纯的职责链模式,android 的点击事件分发模式
*/
public class ChainOfResponsibilityPattern {
public static void main(String[] main) {
PM pm = new PM("addison", null);
UIEngineer ue = new UIEngineer("tonny", pm);
TestEngineer te = new TestEngineer("lihong", ue);
TeamLeader tl = new TeamLeader("jhon", te);
Monkey monkey = new Monkey("tory", tl);
monkey.handleRequest(new AppInfo("jumeiapp", 48, "UI ok", "Code ok"));
monkey.handleRequest(new AppInfo("jumei", 48, "UI terry", "Code ok"));
}
}
//请求的表单内容,根据业务各所不同
class AppInfo {
String appName;
int appSize;
String appPic;
String appCode;
AppInfo(String appName, int appSize, String appPic, String appCode) {
this.appCode = appCode;
this.appName = appName;
this.appPic = appPic;
this.appSize = appSize;
}
}
//抽象处理者
abstract class Handler {
protected String name;
protected Handler nextHandler;
Handler(String name, Handler next) {
this.name = name;
this.nextHandler = next;
}
public void setNextHandler(Handler next) {
this.nextHandler = next;
}
abstract void handleRequest(AppInfo app);
}
//程序员处理
class Monkey extends Handler {
Monkey(String name, Handler next) {
super(name, next);
}
@Override
void handleRequest(AppInfo app) {
System.out.println("Monkey " + name + " coding " + app.appName + " pass");
if (null != nextHandler) {
nextHandler.handleRequest(app);
}
}
}
//ui设计工程师
class UIEngineer extends Handler {
UIEngineer(String name, Handler next) {
super(name, next);
}
@Override
void handleRequest(AppInfo app) {
if ("UI ok".equalsIgnoreCase(app.appPic)) {
System.out.println("UIEngineer " + name + " pass " + app.appName);
if (null != nextHandler) {
nextHandler.handleRequest(app);
}
} else {
System.out.println("UIEngineer " + name + " not pass " + app.appName);
}
}
}
//测试工程师
class TestEngineer extends Handler {
TestEngineer(String name, Handler next) {
super(name, next);
}
@Override
void handleRequest(AppInfo app) {
if (app.appSize < 50) {
System.out.println("TestEngineer " + name + " pass " + app.appName);
if (null != nextHandler) {
nextHandler.handleRequest(app);
}
} else {
System.out.println("TestEngineer " + name + " not pass " + app.appName);
}
}
}
//领导
class TeamLeader extends Handler {
TeamLeader(String name, Handler next) {
super(name, next);
}
@Override
void handleRequest(AppInfo app) {
if ("Code ok".equalsIgnoreCase(app.appCode)) {
System.out.println("TeamLeader " + name + " pass " + app.appName);
if (null != nextHandler) {
nextHandler.handleRequest(app);
}
} else {
System.out.println("TeamLeader " + name + " not pass " + app.appName);
}
}
}
//产品
class PM extends Handler {
PM(String name, Handler next) {
super(name, next);
}
@Override
void handleRequest(AppInfo app) {
if ("Code ok".equalsIgnoreCase(app.appCode)) {
System.out.println("PM " + name + " pass " + app.appName);
if (null != nextHandler) {
nextHandler.handleRequest(app);
}
} else {
System.out.println("PM " + name + " not pass " + app.appName);
}
}
}
内容打印
Hello World!
Monkey tory coding jumeiapp pass
TeamLeader jhon pass jumeiapp
TestEngineer lihong pass jumeiapp
UIEngineer tonny pass jumeiapp
PM addison pass jumeiapp
Monkey tory coding jumei pass
TeamLeader jhon pass jumei
TestEngineer lihong pass jumei
UIEngineer tonny not pass jumei