责任链模式
- 责任链,将功能的实现抽象成一条流水线(执行链),流水线上有多个节点,每个节点都有特定的职责,按特定的工序执行处理。如果某个节点处理完了就可以根据实际业务需求传递给下一个节点继续处理或者返回处理完毕。
构建过程
- 将责任进行分类,抽象出责任接口。
- 根据责任接口实现多个责任类。
每个责任类可通过Set方法更新处理过的对象。
责任类可定义enable变量,实现责任是否启用。 - 定义责任链类。
责任链实现add方法将单个责任添加到链中(可通过链表)。add方法可使用Builder模式,这样实现链式添加方法。
责任链类实现责任接口,实现在责任链中多个责任功能的处理。
实现责任接口同时支撑多链间的Add连接。 - 使用责任链。
创建采用责任链进行处理的业务对象。
创建责任链对象M,创建责任对象X,将多个责任X通过add方法加入到责任链M中。
调用责任链处理方法,传入业务对象进行处理。
使用场景
- 拦截器、过滤器。例如: Http服务器中,请求中一系列处理过程可通过责任链实现。(Struts 的 Filter, Netty 的 ChannelHandler )
- 使用流程化方法、流水线的地方都可以采用责任链模式。
DEMO
一个稍微小复杂的DEMO:使用职责链模式生产车。
主方法类:
//实现责任链模式
//场景:模拟汽车生产流水线,包括的功能责任有:加轮胎、加外壳、喷漆、清洗
//责任链特性包括:
//1.每个责任可以指定开启或关闭
//2.单个责任可以加入到链中
//3.链和链也可以连接形成一个完整的链
public class ResChain_v2 {
public static void main(String[] args) {
//出厂一个底盘,未装配状态
CarChassis myCar = new CarChassis();
//打印未组装情况下的汽车,只有底盘
System.out.println(myCar);
//定义生产链
ProductChain chain = new ProductChain();
//包括 装外壳
chain.addAssembleProcess(new AssembleShell(new CarShell("Japan","Close")),true)
//装轮子
.addAssembleProcess(new AssembleWheel(new CarWheel("Toyota",300)),true)
//喷漆
.addWateringProcess(new PaintColor("Red"),true)
//洗车
.addWateringProcess(new CleanCar(),true);
//按生产链生产一辆车,并打印
chain.Product(myCar);
System.out.println(myCar);
//再来一辆新车,对生产链进行测试
CarChassis yourCar = new CarChassis();
ProductChain mainChain = new ProductChain(); //主生产链
ProductChain asssembleChain = new ProductChain();// 组装生产链:装轮子、装外壳
ProductChain wateringChain = new ProductChain(); // 水化作业生产链:喷漆、洗车
//组装生产链
asssembleChain.addAssembleProcess(new AssembleShell(new CarShell("Audi","Open")),true)
.addAssembleProcess(new AssembleWheel(new CarWheel("Germany",500)),false);
//水化作业生产链
wateringChain.addWateringProcess(new PaintColor("Black"),true)
.addWateringProcess(new CleanCar(),false);
//两条生产链加入主生产链
mainChain.addAssembleProcess(asssembleChain,true)
.addWateringProcess(wateringChain,true);
//出车
mainChain.Product(yourCar);
System.out.println(yourCar);
}
}
汽车底盘类:
//车的底盘类,有四个属性,需要都装上才是完整的车
public class CarChassis {
private CarChassis baseCar;
public CarChassis()
{
baseCar = this;
}
public void SetBaseCar(CarChassis baseCar)
{
this.baseCar = baseCar;
}
public CarShell getShell() {
return shell;
}
public void setShell(CarShell shell) {
this.shell = shell;
}
private CarShell shell;
public CarWheel getWheel() {
return wheel;
}
public void setWheel(CarWheel wheel) {
this.wheel = wheel;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
private CarWheel wheel;
public Condition getCon() {
return con;
}
public void setCon(Condition con) {
this.con = con;
}
private String color;
private Condition con = Condition.DIRTY;
public static enum Condition
{
CLEAN,DIRTY;
}
@Override
public String toString() {
return "CarChassis{" +
"shell=" + shell +
", wheel=" + wheel +
", color='" + color + '\'' +
", con=" + con +
'}';
}
}
车外壳类:
//车外壳实体类
public class CarShell {
public String from = "China";
public String type = "default";
public CarShell(){}
public CarShell(String from, String type) {
this.from = from;
this.type = type;
}
@Override
public String toString() {
return "CarShell{" +
"from='" + from + '\'' +
", type='" + type + '\'' +
'}';
}
}
车轮类:
//车轮实体类
public class CarWheel {
public String brand = "default";
public int size = 0;
public CarWheel(){};
public CarWheel(String brand,int size) {
this.size = size;
this.brand = brand;
}
@Override
public String toString() {
return "CarWheel{" +
"brand='" + brand + '\'' +
", size=" + size +
'}';
}
}
责任类,装外壳:
//责任类:装外壳的类
public class AssembleShell implements Assembility {
private CarShell shell;
public AssembleShell(CarShell shell) {
this.shell = shell;
}
@Override
public void Assemble(CarChassis chassis, Boolean enable) {
if(enable) {
chassis.setShell(shell);
}
chassis.SetBaseCar(chassis);
}
}
责任类,装轮子
//责任链:装轮子的类
public class AssembleWheel implements Assembility {
private CarWheel wheel;
public AssembleWheel(CarWheel wheel) {
this.wheel = wheel;
}
@Override
public void Assemble(CarChassis chassis, Boolean enable) {
if(enable) {
chassis.setWheel(wheel);
}
chassis.SetBaseCar(chassis);
}
}
//抽象责任链接口,可组装的,包括装轮子、装外壳
public interface Assembility {
void Assemble(CarChassis chassis,Boolean enable);
}
责任类,喷漆:
责任链:喷漆
public class PaintColor implements Waterbility{
private String colour;
public PaintColor(String colour) {
this.colour = colour;
}
@Override
public void Watering(CarChassis chassis, Boolean enable) {
if(enable) {
chassis.setColor(colour);
}
chassis.SetBaseCar(chassis);
}
}
责任类,洗车:
//责任链:洗车
public class CleanCar implements Waterbility{
@Override
public void Watering(CarChassis chassis, Boolean enable) {
if(enable) {
chassis.setCon(CarChassis.Condition.CLEAN);
}
chassis.SetBaseCar(chassis);
}
}
//责任链抽象接口,水化作业,包括:喷漆和洗车两种
public interface Waterbility {
void Watering(CarChassis chassis,Boolean enable);
}
责任类实现类:
//生产链,将装轮子、装外壳、喷漆、洗车 四种责任封装
public class ProductChain implements Assembility,Waterbility{
private List<Assembility> processes_a = new LinkedList<Assembility>();
private List<Waterbility> processes_b = new LinkedList<Waterbility>();
ProductChain addAssembleProcess(Assembility process, Boolean enable)
{
if(enable)
processes_a.add(process);
return this;
}
ProductChain addWateringProcess(Waterbility process, Boolean enable)
{
if(enable)
processes_b.add(process);
return this;
}
//生产一辆车
public void Product(CarChassis chassis)
{
this.Assemble(chassis,true);
this.Watering(chassis,true);
}
@Override
public void Assemble(CarChassis chassis, Boolean enable) {
if(enable)
{
for (Assembility process: processes_a)
{
process.Assemble(chassis,enable);
}
}
}
@Override
public void Watering(CarChassis chassis, Boolean enable) {
if(enable)
{
for (Waterbility process: processes_b)
{
process.Watering(chassis,enable);
}
}
}
}
优点
- 增强扩展性。可以根据需要增加新的请求处理类,处理类之间解耦,满足开闭原则。
- 当工作流程发生变化,可动态地新增或者删除责任。
- 处理类责任范围明确,符合类的单一职责原则。