Responsibility Chain Mode for Java mode development, java mode development

Source: Internet
Author: User

Responsibility Chain Mode for Java mode development, java mode development

Responsibility Chain Mode for Java mode development


Starting from drumming and spreading flowers

Drumming is a lively and intense drinking game. At the banquet, guests are arranged in sequence to drum up one person. The drums are separated from the flowers to show justice. When the drum is started, the bouquet begins to pass in sequence, and the sound of a drum falls. If the bouquet is in the hands of someone, the person will have to drink.

Phabi said that Jia Mu, Jia Yu, Jia Zheng, Jia Baoyu, and Jia Huan are the five painters who participate in the drum-and-flower games. They form a chain link. The tambourine passes the flowers to Jia mu to start the flower games. The flowers were transmitted from Jia mu to Jia Yu, and from Jia Zheng to Jia Baoyu, and from Jia Baoyu to Jia Huan, and from Jia Huan to Jia Mu ). When the drums stop, the flowers in their hands will have to execute the command.



It is the application of the responsibility chain model. In the responsibility chain mode, many objects are connected by each object's reference to its next home to form a chain. Requests are transmitted on this chain until an object on the chain decides to process this request. The client that sends this request does not know which object on the chain will eventually process this request, which allows the system to dynamically reorganize the chain and assign responsibility without affecting the client.

A chain of responsibility may be a straight line, a chain, or even a part of a tree structure.

Structure of the responsibility chain model

The responsibility chain mode is an object behavior mode. Its roles are as follows:

First, the abstract Handler (Handler) role defines an interface for processing requests. If necessary, the interface can define a method to return a reference to the next house. A schematic class diagram is provided:


Figure 2. Abstract processor role.

The Accumulation Relationship in the figure provides references to sub-classes. The abstract method handleRequest () standardizes the sub-classes to process requests.

2. The specific handler (ConcreteHandler) role. After receiving the request, you can choose to process the request or send the request to the next user. A schematic class diagram is provided.


Figure 3. Specific processor role.


The ConcreteHandler class of the schematic in only has one handleRequest () method.

The static class structure of the responsibility chain mode is visible:



Figure 4. Define the class diagram of the responsibility chain mode.

Pure and non-pure responsibility chain model

A pure responsibility chain model requires that a specific handler object can only select one of the two actions: one is responsible, and the other is responsible for the next. It is not allowed that a specific processor object transfers down the responsibility after assuming part of the responsibility.

In a pure mode of responsibility chain, a request must be accepted by a handler object. In an unpure mode of responsibility chain, A request cannot be accepted by any receiver object.

The actual examples of the pure responsibility chain mode are difficult to find. Generally, the examples are the implementation of the pure responsibility chain mode. Some people think that the responsibility chain is not the responsibility chain model at all, which may be justified; but in the actual system, the pure responsibility chain is hard to find; if the responsibility chain is not purely the responsibility chain model, the responsibility chain model will not make much sense.

The responsibility chain mode requires that all objects on the chain inherit from a common parent class.
  

Under what circumstances should I use the responsibility chain model?

Use the responsibility chain mode in the following situations:

First, the system already has a chain composed of processor objects. This chain may be given in the composite mode,

First, when there is more than one processor object, it will process one request, and it is unknown in advance which processor object will process one request. The handler object is dynamically determined.

2. When the system wants to send a request to one of multiple handler objects, it does not explicitly specify which handler object will process the request.

Third, when the set of handler objects processing a request needs to be dynamically specified.

Strengths and weaknesses of using the responsibility chain model

The responsibility chain mode reduces the coupling between the objects that issue the command and the objects that process the command. It allows multiple processor objects to decide which processor finally processes the command based on their own logic. In other words, the object that sends a command only transmits the command to the start of the chain structure, without knowing which node on the chain has processed the command.

Obviously, this means that the system can have more flexibility in processing commands. Which object finally processes a command may vary with the objects involved in the responsibility chain and their locations on the responsibility chain.

Implementation of the responsibility chain model

The origin of the Chain Structure

It is worth noting that the responsibility chain model does not create a responsibility chain. The responsibility chain must be created in other parts of the system.

The responsibility chain mode reduces the coupling between the sending end and the receiving end of the request, so that multiple objects have the opportunity to process the request. A chain can be a line, a tree, or a ring. The topological structure of a chain can be single-connected or multi-connected. The responsible chain mode does not specify the topological structure of the responsible chain. However, the responsibility chain mode requires that commands can only be passed to one home (or processed) at the same time, but not to more than one home. In the following figure, the responsibility chain is part of a tree structure.



Figure 9. The responsibility chain is part of the existing tree structure of the system. The Shadow object in the figure provides a possible command propagation path.

The members of the responsibility chain are often part of a larger structure. For example, all the Members in the drum-and-teaser games in the Dream of Red Mansions discussed earlier are members of Jia Fu. If the members of the responsible chain do not exist, they must be created to use the responsible chain mode. The specific handler object of the responsible chain can be an instance of the same specific handler class.

In the AWT event processing model of Java 1.0, the responsibility chain is the container hierarchy of components on the windows.

In the dhtml dom Event Processing Model of Internet Explorer described below, the responsibility chain is the DOM level structure itself.

Command Transmission

There may be more than one command passed on a chain of responsibility, but several commands. These commands can be implemented through polymorphism at the abstract layer and the specific layer. For details, see. This allows you to separate the responsibility between the command object and the object on the responsibility chain, and separate the command objects from the objects that spread the command.



Figure 10. propagation of multiple commands on the responsibility chain.

Example:

 

FilterChain manages the entire Filter and inherits the Filter itself, so that you can attach other FilterChain

public class FilterChain implements Filter {List<Filter> filters = new ArrayList<Filter>();private int index = 0;public FilterChain addFilter(Filter f) {filters.add(f);return this;}


 

 

public void doFilter(Request request, Response response, FilterChain chain) {if(index == filters.size()) return;Filter f = filters.get(index);index ++;f.doFilter(request, response, chain);}} public class HTMLFilter implements Filter { public void doFilter(Request request, Response response, FilterChain chain) {request.setRequestStr(request.getRequestStr().replace('<', '['));request.setRequestStr(request.getRequestStr().replace('>', ']') + "---HTMLFilter()");chain.doFilter(request, response, chain);response.setResponseStr(response.getResponseStr() + "---HTMLFilter()");} }


 

 

This document provides references.

Source: yesky responsible editor: Ark

Copyright Disclaimer: you are welcome to reprint it. I hope you can add the original article address while reprinting it. Thank you for your cooperation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.