Android 设计模式

设计模式之责任链模式

2023-02-13  本文已影响0人  Tinyspot

责任链模式

优点:职责链模式使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系

1.2 两个角色

(1)抽象处理者(Handler):定义处理请求的方法,并维护一个下一处理节点Handler对象的引用
(2)具体处理者(ConcreteHandler):对请求进行处理,如果不感兴趣,则进行转发

2. 示例

用户的请求传递,直到可以解决这个请求为止

image.png
public abstract class AbstractHandler {

    protected AbstractHandler successor;
    /**
     * 设置继任者
     */
    public void setSuccessor(AbstractHandler successor) {
        this.successor = successor;
    }

    protected abstract void doHandle(String request);
}
public class ConcreteHandlerA extends AbstractHandler {
    @Override
    protected void doHandle(String request) {
        if (request.startsWith("handlerA")) {
            System.out.println("ConcreteHandlerA execute: " + request);
        } else {
            // 请求向下传递
            successor.doHandle(request);
        }
    }
}

public class ConcreteHandlerB extends AbstractHandler {
    @Override
    public void doHandle(String request) {
        if (request.startsWith("handlerB")) {
            System.out.println("ConcreteHandlerB execute: " + request);
        } else {
            successor.doHandle(request);
        }
    }
}

public class ConcreteHandlerC extends AbstractHandler {
    @Override
    protected void doHandle(String request) {
        if (request.startsWith("handlerC")) {
            System.out.println("ConcreteHandlerC execute: " + request);
        } else {
            successor.doHandle(request);
        }
    }
}

测试:

@Test
public void test() {
    AbstractHandler handlerA = new ConcreteHandlerA();
    AbstractHandler handlerB = new ConcreteHandlerB();
    AbstractHandler handlerC = new ConcreteHandlerC();
    handlerA.setSuccessor(handlerB);
    handlerB.setSuccessor(handlerC);

    List<String> list = Arrays.asList("handlerA000", "handlerB111", "handlerC222");
    for (String str : list) {
        handlerA.doHandle(str);
    }
}
``
## 3. 其他
```java
public abstract class AbstractHandle {

    public static void main(String[] args) {
        AbstractHandle handle = Builder.create()
                .addHandler(new ValidateHandle())
                .addHandler(new LoginHandle())
                .addHandler(new AuthHandle())
                .build();
        handle.doHandle("111");
    }

    protected AbstractHandle chain;

    public void next(AbstractHandle handle) {
        this.chain = handle;
    }

    /**
     * 抽象方法
     * @param request
     */
    public abstract void doHandle(String request);

    public static class Builder {
        private AbstractHandle head;
        private AbstractHandle tail;

        public Builder addHandler(AbstractHandle handle) {
            if (this.head == null) {
                this.head = this.tail = handle;
                return this;
            }
            this.tail.next(handle);
            this.tail = handle;
            return this;
        }

        public AbstractHandle build() {
            return this.head;
        }

        public static Builder create() {
            return new Builder();
        }
    }
}

class ValidateHandle extends AbstractHandle {
    @Override
    public void doHandle(String request) {
        System.out.println("ValidateHandle start");
        if (chain != null) {
            chain.doHandle(request);
        }
    }
}

class LoginHandle extends AbstractHandle {
    @Override
    public void doHandle(String request) {
        System.out.println("LoginHandle start");
        if (chain != null) {
            chain.doHandle(request);
        }
    }
}

class AuthHandle extends AbstractHandle {
    @Override
    public void doHandle(String request) {
        System.out.println("AuthHandle start");
        // 若还有下一级,请求继续传递
        if (chain != null) {
            chain.doHandle(request);
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读