设计模式之责任链模式

package com.tuling.designpattern;

/**
 * 责任链模式
 * 应用场景:一个请求的处理需要多个对象中的一个或几个协作处理
 * 优点:
 * 1.请求的发送者和接收者解耦
 * 2.可以控制执行顺序
 * 3.符合开闭原则和单一职责原则
 */
public class chainofresponsibility {
    public static void main(String[] args) {

        Request request = new Request.RequestBuilder().frequentOk(true).loggendOn(false).build();
        RequestFrequentHandler requestFrequentHandler = new RequestFrequentHandler(new LoggingHandler(null));

        if(requestFrequentHandler.process(request)){
            System.out.println("正常业务处理");
        }else{
            System.out.println("访问异常");
        }
    }
}
class Request{
    //是否登录
    private boolean loggendOn;
    //访问频率是否ok
    private boolean frequentOk;
    //是否授权
    private boolean isPermits;
    //是否包含敏感词汇
    private boolean contaisSensitiveWords;

    private String requestBody;

    public Request(boolean loggendOn, boolean frequentOk, boolean isPermits, boolean contaisSensitiveWords) {
        this.loggendOn = loggendOn;
        this.frequentOk = frequentOk;
        this.isPermits = isPermits;
        this.contaisSensitiveWords = contaisSensitiveWords;
    }
    static class RequestBuilder{
        private boolean loggendOn;
        //访问频率是否ok
        private boolean frequentOk;
        //是否授权
        private boolean isPermits;
        //是否包含敏感词汇
        private boolean contaisSensitiveWords;

        RequestBuilder loggendOn(boolean loggendOn){
            this.loggendOn=loggendOn;
            return this;
        }
        RequestBuilder frequentOk(boolean frequentOk){
            this.frequentOk=frequentOk;
            return this;
        }
        RequestBuilder isPermits(boolean isPermits){
            this.isPermits=isPermits;
            return this;
        }
        RequestBuilder contaisSensitiveWords(boolean contaisSensitiveWords){
            this.contaisSensitiveWords=contaisSensitiveWords;
            return this;
        }
        public Request build(){
            Request request = new Request(loggendOn,frequentOk,isPermits,contaisSensitiveWords);
            return request;
        }
    }

    public boolean isLoggendOn(){
        return loggendOn;
    }

    public boolean isFrequentOk(){
        return frequentOk;
    }

    public boolean isPermits(){
        return isPermits;
    }

    public boolean isContaisSensitiveWords(){
        return contaisSensitiveWords;
    }
}

abstract class Handler{
    Handler next;

    public Handler(Handler next) {
        this.next = next;
    }

    public Handler getNext() {
        return next;
    }

    public void setNext(Handler next) {
        this.next = next;
    }

    abstract boolean process(Request request);
}
class RequestFrequentHandler extends Handler{

    public RequestFrequentHandler(Handler next) {
        super(next);
    }

    @Override
    boolean process(Request request) {
        System.out.println("访问频率控制.");
        if(request.isFrequentOk()){
            Handler next=getNext();
            if(next == null){
                return true;
            }
            if(!next.process(request)){
                return false;
            }else{
                return true;
            }
        }
        return false;
    }
}

class LoggingHandler extends Handler{

    public LoggingHandler(Handler next) {
        super(next);
    }

    @Override
    boolean process(Request request) {
        System.out.println("登录验证");
        if(request.isLoggendOn()){
            Handler next=getNext();
            if(next == null){
                return true;
            }
            if(!next.process(request)){
                return false;
            }else{
                return true;
            }
        }
        return false;
    }
}

你可能感兴趣的:(设计模式,责任链模式,java)