定义: 为了避免请求发送者与多个请求处理者耦合在一起,将所有请求的处理者通过前一对象记住其下一个对象的引用而连成一条链;当有请求发生时,可将请求沿着这条链传递,直到有对象处理它为止
结构模型
本质是当前处理类持有下一个处理类的引用, 处理自己的任务,并交给下一个引用处理
使用场景: OkHttp中的拦截器, android的事件分发和消费
举个例子: 设置desk的形状, 尺寸, 颜色, 透明度属性
这里模仿Okhttp的责任链模式来实现这一功能
思路:
1.创建一个抽象处理接口, 链Chain作为形参, desk作为返回值
public interface Decor {
/**
*
* @param chain
* @param preDesk
* @return
*/
Desk proceed(Chain chain, Desk preDesk);
}
Chain作为形参的目的是, 获取处理类对象统一 在Chain实现类中获取
2.创建Chain接口, 通过chain去调用处理类的方法,并返回desk对象
public interface Chain {
/**
* 获取任务属性
*
* @return
*/
TaskAttribute carry();
/**
* 获取Desk
* @param carrier 任务
* @param preDesk 前一个desk
* @return
*/
Desk proceed(TaskAttribute carrier, Desk preDesk);
}
Chain的实现类
public class RealDecorChain implements Chain {
//下标
private final int index;
//处理类集合
private final List<Decor> mDecorList;
//任务属性
private final TaskAttribute mCarrier;
public RealDecorChain(int index, List<Decor> decorList, TaskAttribute carrier) {
this.index = index;
this.mDecorList = decorList;
this.mCarrier = carrier;
}
@Override
public TaskAttribute carry() {
return mCarrier;
}
@Override
public Desk proceed(TaskAttribute carrier, Desk preDesk) {
if (index >= mDecorList.size()) return preDesk;
//获取集合中的处理类对象
Decor decor = mDecorList.get(index);
RealDecorChain nextChain = new RealDecorChain(index + 1, mDecorList, mCarrier);
Desk desk = decor.proceed(nextChain, preDesk);
return desk;
}
}
3.分别设置desk的属性
Shape(形状)
public class ShapeDecor implements Decor {
@Override
public Desk proceed(Chain chain, Desk preDesk) {
TaskAttribute task = chain.carry();
Desk dimenDesk = chain.proceed(task, preDesk);
if (dimenDesk != null) {
dimenDesk.setShape(task.getShape());
return dimenDesk;
}
//设置桌子的形状
Desk shapeDesk = new Desk();
shapeDesk.setShape(task.getShape());
return shapeDesk;
}
}
Dimen(尺寸)
public class DimenDecor implements Decor {
@Override
public Desk proceed(Chain chain, Desk preDesk) {
TaskAttribute task = chain.carry();
Desk colorDesk = chain.proceed(task, preDesk);
if (colorDesk != null) {
colorDesk.setSize(task.size());
return colorDesk;
}
Desk dimenDesk = new Desk();
dimenDesk.setSize(task.size());
return dimenDesk;
}
}
Color(颜色)
public class ColorDecor implements Decor {
@Override
public Desk proceed(Chain chain, Desk preDesk) {
TaskAttribute task = chain.carry();
String color = task.getColor();
Desk alphaDesk = chain.proceed(task, preDesk);
if (alphaDesk != null) {
alphaDesk.setColor(color);
return alphaDesk;
}
Desk colorDesk = new Desk();
colorDesk.setColor(color);
return colorDesk;
}
}
Alpha(透明度)
public class AlphaDecor implements Decor {
@Override
public Desk proceed(Chain chain, Desk preDesk) {
TaskAttribute task = chain.carry();
float alpha = task.alpha();
if (alpha < 0 || alpha > 1) {
Desk desk = chain.proceed(task, preDesk);
if (desk != null) {
desk.setAlpha(alpha);
return desk;
}
}
Desk alphaDesk = new Desk();
alphaDesk.setAlpha(alpha);
return alphaDesk;
}
}
TaskAttribute(任务)
public class TaskAttribute {
//形状
private String[] shapes = {"circle", "oval", "rect"};
//尺寸
private int[] dimens = {20, 30, 40};
//颜色
private String[] colors = {"red", "blue"};
//透明度
private float[] alpha = {0.3f, 0.8f};
private int index;
public TaskAttribute() {
Random rd = new Random();
index = rd.nextInt(shapes.length);
}
public String getShape() {
if (index >= shapes.length) return null;
return shapes[index];
}
public int size() {
if (index >= dimens.length) return 0;
return dimens[index];
}
public String getColor() {
if (index >= colors.length) return null;
return colors[index];
}
public float alpha() {
if (index >= alpha.length) return 0;
return alpha[index];
}
}
Desk类 包含了形状,尺寸,颜色,透明度等属性
将所有的处理类节点链接起来, 通过RealDecorChain分发任务给每个节点
List<Decor> mDecorList = new ArrayList<>();
mDecorList.add(new ShapeDecor());
mDecorList.add(new DimenDecor());
mDecorList.add(new ColorDecor());
mDecorList.add(new AlphaDecor());
TaskAttribute carrier = new TaskAttribute();
RealDecorChain chain = new RealDecorChain(0, mDecorList, carrier);
Desk desk = chain.proceed(carrier, null);
System.out.println(desk);
//打印结果
Desk{color='red', alpha=0.3, shape='circle', size=20}
优点:
1.降低对象之间的耦合度,无须知道哪个对象处理其请求
2.简化了对象之间的关系,每个对象只要持有一个后续处理类的引用,避免了众多的 if 或者 else
3.责任分担,每个处理类只要处理它本身的工作,不该处理的传递给下一个对象处理
4.增强了系统的可扩展性,根据需求添加新的处理类
网友评论