Java Design Pattern--Chain of Responsibility Pattern

1 Chain of Responsibility Pattern

Purpose: To weaken the relationship between the requester and the handler, so that both parties can become reusable components;
Implementation: The intercepted classes all implement a unified interface, and multiple objects are formed into a chain of responsibility, and then follow the order of their responsibilities. One by one, find out who is responsible for dealing with it.

1. The handler on the chain of responsibility is responsible for processing the request, and the client only needs to send the request to the chain of responsibility, and does not need to care about the processing details of the request and the delivery of the request;
2. Each receiver contains information about another receiver. Quote. If an object cannot handle the request, it will pass the same request to the next recipient, and so on.

2 Implementation

Code scene: In the mobile phone production line workshop, the assembly line workers of different components will install components such as cpu, camera, and ram.
1. The processed object is the mobile phone ;
2. The abstract handler is the assembly line ;
3. The concrete handler is the worker of different components .

2.1 Code Implementation

Abstract handler role: AssemblyWorker (assembly worker)

public abstract class AssemblyWorker {
    //责任链关键 下一个工人
    protected AssemblyWorker nextWorker;
    protected MobilePhone mobilePhone;

    public AssemblyWorker(MobilePhone mobilePhone) {
        this.mobilePhone = mobilePhone;
    }
    public abstract void assembling();
    //设定下一个工人
    public void setNextWorker(AssemblyWorker nextWorker) {
        this.nextWorker = nextWorker;
    }

}

Specific processor role: CpuWorker (CPU assembly worker)

public class CpuWorker extends AssemblyWorker {

    private final String name = "CPU组装工人";
    public CpuWorker(MobilePhone mobilePhone) {
        super(mobilePhone);
    }

    @Override
    public void assembling() {
        if (null == mobilePhone.getCpu()) {
            mobilePhone.setCpu("高通骁龙8");
            System.out.println(name + "将CPU给手机组装上了");
        }
        if (null != nextWorker) {
            nextWorker.assembling();
        }
    }
}

Specific processor role: CpuWorker (camera assembly worker)

public class CameraWorker extends AssemblyWorker {

    private final String name = "摄像头组装工人";
    public CameraWorker(MobilePhone mobilePhone) {
        super(mobilePhone);
    }

    @Override
    public void assembling() {
        if (null == mobilePhone.getCamera()) {
            mobilePhone.setCamera("索尼2400万像素摄像头");
            System.out.println(name + "将摄像头给手机组装上了");
        }
        if (null != nextWorker) {
            nextWorker.assembling();
        }
    }
}

Specific processor role: RamWorker (running memory assembly worker)

public class RamWorker extends AssemblyWorker {

    private final String name = "运行内存组装工人";

    public RamWorker(MobilePhone mobilePhone) {
        super(mobilePhone);
    }

    @Override
    public void assembling() {
        if (null == mobilePhone.getRam()) {
            mobilePhone.setRam("DDR4 8G 运行内存");
            System.out.println(name + "运行内存给手机组装上了");
        }
        if (null != nextWorker) {
            nextWorker.assembling();
        }
    }
}

2.2 Involving roles

The Chain of Responsibility pattern structure diagram includes the following roles:

Handler (abstract handler): It defines an interface for processing requests, which is generally designed as an abstract class. Since different concrete handlers handle requests in different ways, abstract request processing methods are defined in it. Because the next home of each processor is still a processor, an object of abstract processor type (such as successor in the structure diagram) is defined in the abstract processor as its reference to the next home. Through this reference, handlers can be linked together in a chain.

ConcreteHandler (concrete handler): It is a subclass of the abstract handler and can process user requests. The abstract request processing method defined in the abstract handler is implemented in the concrete handler class. Before processing the request, it needs to be judged to see if Has the corresponding processing authority, if the request can be processed, it will be processed, otherwise the request will be forwarded to the successor; in the specific processor, the next object in the chain can be accessed for the forwarding of the request.

2.3 call

caller:

public class Client {
    public static void main(String[] args) {
        //对一个什么都没有的手机进行组装
        MobilePhone mp = new MobilePhone();
        AssemblyWorker cpuWorker = new CpuWorker(mp);
        AssemblyWorker cameraWorker = new CameraWorker(mp);
        AssemblyWorker ramWorker = new RamWorker(mp);
        cpuWorker.setNextWorker(cameraWorker);
        cameraWorker.setNextWorker(ramWorker);

        cpuWorker.assembling();
        mp.show();
        System.out.println("-------------------------");

        //对一个已经存在CPU的手机进行组装
        MobilePhone mp1 = new MobilePhone();
        mp1.setCpu("Intel");
        AssemblyWorker cpuWorker1 = new CpuWorker(mp1);
        AssemblyWorker cameraWorker1 = new CameraWorker(mp1);
        AssemblyWorker ramWorker1 = new RamWorker(mp1);
        cpuWorker1.setNextWorker(cameraWorker1);
        cameraWorker1.setNextWorker(ramWorker1);

        cpuWorker1.assembling();
        mp1.show();
    }
}

result:

CPU组装工人将CPU给手机组装上了
摄像头组装工人将摄像头给手机组装上了
运行内存组装工人运行内存给手机组装上了
该手机的CPU为[高通骁龙8],Camera为[索尼2400万像素摄像头],RAM为[DDR4 8G 运行内存]
-------------------------
摄像头组装工人将摄像头给手机组装上了
运行内存组装工人运行内存给手机组装上了
该手机的CPU为[Intel],Camera为[索尼2400万像素摄像头],RAM为[DDR4 8G 运行内存]

Code address: click to jump

References:
[1] Graphical Design Patterns/(Japanese) Hiroshi Yuki; translated by Yang Wenxuan. – Beijing: People’s Posts and Telecommunications Press, 2017.1.
[ 2 ] Wikipedia Design
Patterns [ 3 ] Geek Academy WIKI – Design Patterns .
[ 4 ] Rookie Tutorial – Design Patterns .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326127281&siteId=291194637