设计模式—职责链

此例中涉及到的类有抽象处理者、具体处理者(中间处理者与终结处理者)、处理链等主要类,请求、审批标准、审批结果等辅助类 ,下列是全部代码及其简要说明。

目录

1、处理者类

2、价格处理类

3、库存处理类

4、质量处理类

5、审批通过处理类

6、处理链类

7、请求类

8、审批标准类

 9、审批结果类

10、客户端

 11、运行结果


 

1、处理者类

充当抽象处理者,维护一个自身类型的引用,用于构造处理链,接收请求对象、标准对象,返回处理结果

package uml.behavioral.responsibility_chain.ext;

/**
 * 职责链--抽象请求处理者
 * on 2023/2/1 14:26
 */
public abstract class Handler {
    protected Handler nextHandler;
    abstract Result handle(Request request,Standard standard);

    public void setNextHandler(Handler nextHandler) {
        this.nextHandler = nextHandler;
    }
}

 2、价格处理类

充当具体处理者

package uml.behavioral.responsibility_chain.ext;

/**
 * 职责链--具体请求处理者[中间处理者]
 * on 2023/2/1 14:42
 */
public class PriceHandler extends Handler {
    @Override
    Result handle(Request request, Standard standard) {
        if (request.getPrice() > standard.getMaxPrice())
            return new Result("不通过", standard.getPriceDesc());
        return nextHandler.handle(request, standard);
    }
}

3、库存处理类

充当具体处理者

package uml.behavioral.responsibility_chain.ext;

/**
 * 职责链--具体请求处理者[中间处理者]
 * on 2023/2/1 14:42
 */
public class StockHandler extends Handler {
    @Override
    Result handle(Request request, Standard standard) {
        if (request.getStock() < standard.getMinStock())
            return new Result("不通过", standard.getStockDesc());
        return nextHandler.handle(request, standard);
    }
}

4、质量处理类

充当具体处理者

package uml.behavioral.responsibility_chain.ext;

/**
 * 职责链--具体请求处理者[中间处理者]
 * on 2023/2/1 14:43
 */
public class QualityHandler extends Handler {
    @Override
    Result handle(Request request, Standard standard) {
        if (request.getQuality() < standard.getMinQuality())
            return new Result("不通过", standard.getQualityDesc());
        return nextHandler.handle(request, standard);
    }
}

5、审批通过处理类

在本例中,充当终结处理者,不对请求处理,只返回最终结果

package uml.behavioral.responsibility_chain.ext;

/**
 * 职责链--具体请求处理者[终结处理者]
 * on 2023/2/1 14:50
 */
public class PassedHandler extends Handler {
    @Override
    Result handle(Request request, Standard standard) {
        return new Result("通过", "请求的所有条件均符合要求,予以通过");
    }
}

 6、处理链类

借助处理者数组构建处理链,接收请求与标准,返回处理结果

package uml.behavioral.responsibility_chain.ext;

/**
 * 职责链--处理链:借助一个个的具体处理者构造处理链,最好配置终结处理者,使用时将请求提交到链上即可
 * on 2023/2/1 14:33
 */
public class HandlerChain {
    private Handler[] handlers;

    /**
     * @param handlers 特别注意:一个处理者数组有且仅有一个终结处理者,并且,终结处理者应位于数组末端
     */
    public HandlerChain(Handler[] handlers) {
        this.handlers = handlers;
        constructChain();
    }

    private void constructChain() {
        for (int i = 0; i < handlers.length - 1; i++) {
            handlers[i].setNextHandler(handlers[i + 1]);
        }
    }

    public Result handleRequest(Request request, Standard standard) {
        return handlers[0].handle(request, standard);
    }
}

 7、请求类

实际过程中,通常有前端表单收集得到

package uml.behavioral.responsibility_chain.ext;

/**
 * 职责链--具体请求【实际环境中可通过表单收集请求数据】
 * on 2023/2/1 14:25
 */
public class Request {
    private int price;//单品价格
    private int stock;//库存量
    private int quality;//质量评分

    public Request(int price, int stock, int quality) {
        this.price = price;
        this.stock = stock;
        this.quality = quality;
    }

    public int getPrice() {
        return price;
    }

    public int getStock() {
        return stock;
    }

    public int getQuality() {
        return quality;
    }
}

8、审批标准类

用于和实际请求做对比

package uml.behavioral.responsibility_chain.ext;

/**
 * 职责链--标准【实际环境中可结合数据库使用、结合前端可动态配置标准】
 * on 2023/2/1 14:25
 */
public class Standard {
    private int maxPrice;//单品最高价格
    private int minStock;//最低库存
    private int minQuality;//最低质量评分
    private String priceDesc;
    private String stockDesc;
    private String qualityDesc;

    public Standard(int price, int stock, int quality, String priceDesc, String stockDesc, String qualityDesc) {
        this.maxPrice = price;
        this.minStock = stock;
        this.minQuality = quality;
        this.priceDesc = priceDesc;
        this.stockDesc = stockDesc;
        this.qualityDesc = qualityDesc;
    }

    public int getMaxPrice() {
        return maxPrice;
    }

    public int getMinStock() {
        return minStock;
    }

    public int getMinQuality() {
        return minQuality;
    }

    public String getPriceDesc() {
        return priceDesc;
    }

    public String getStockDesc() {
        return stockDesc;
    }
    public String getQualityDesc() {
        return qualityDesc;
    }
}

 9、审批结果类

用于表示各个审批节点的状态

package uml.behavioral.responsibility_chain.ext;

/**
 * 职责链--请求审批处理结果
 * on 2023/2/1 14:26
 */
public class Result {
    private String result;//审批处理结果:通过或不通过
    private String detail;//详情

    public Result(String result, String detail) {
        this.result = result;
        this.detail = detail;
    }

    public String getResult() {
        return result;
    }

    public String getDetail() {
        return detail;
    }

    @Override
    public String toString() {
        return "Result{" +
                "result='" + result + '\'' +
                ", detail='" + detail + '\'' +
                '}';
    }
}

10、客户端

基于处理链编程,实际过程中,各个具体的处理者,可结合配置文件与Java反射机制获得。本例中,简单起见,直接构造一个处理者数组。

package client.behavioral.responsibility_chain.ext;

import org.junit.Test;
import uml.behavioral.responsibility_chain.ext.*;

/**
 * 职责链--客户端
 * on 2023/2/1 14:51
 */
public class Client {
    @Test
    public void t() {
        HandlerChain handlerChain = new HandlerChain(
                new Handler[]{new PriceHandler(), new StockHandler(), new QualityHandler(), new PassedHandler()});
        Result result = handlerChain.handleRequest(
                new Request(21, 40, 98),
                new Standard(20, 30, 97,
                        "单品价格定价过高", "商品库存太低", "商品质量不合格"));
        System.out.println(result);
    }
}

 11、运行结果

根据具体请求与请求标准的不同,可获得下列审批结果之一。

Result{result='不通过', detail='单品价格定价过高'}
Result{result='不通过', detail='商品库存太低'}
Result{result='不通过', detail='商品质量不合格'}
Result{result='通过', detail='请求的所有条件均符合要求,予以通过'}

以上客户端运行后,将获得第一种审批结果。

 

感谢阅读,如有任何意见与建议,请留言评论区。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bitDesigner

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值