The responsibility chain model of Java pattern development

Source: Internet
Author: User

The responsibility chain model of Java pattern development


Talking about the flowers from the drums

Drumming is a lively and tense drinking game . In the banquet of guests seated in turn, by a person drumming, drums and the place of the flowers are separated, to show just. When the drums begin, the bouquet begins to pass, the drums fall, and if the bouquet is in someone's hand, the person will have to drink.

False than said, Jiamu, Jia-Pardon, Jia Zheng, Jia Baoyu and Baoyu is five to participate in the Drum pass flower Game of the flower, they form a ring chain. The drum player passes the flowers to Jiamu and begins the flower-spreading game. Flowers from Jiamu to Jia pardon, from Jia pardon passed to Jia Zheng, from Jia Zheng to Jia Baoyu, and from Jia Baoyu to Baoyu, from Baoyu back to Jiamu, so reciprocating (see). When the drums stop, people with flowers in their hands will have to enforce the prohibition.



It is the application of the responsibility chain mode to pass the flower drum. In the chain of responsibility, many objects are joined together by each object's reference to its next-generation chain. The request is passed on this chain until an object on the chain decides to process the request. The client making this request does not know which object on the chain is ultimately processing the request, which allows the system to dynamically reorganize the chain and assign responsibility without affecting the client.

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

The structure of the responsibility chain model

The responsibility chain pattern is an object's behavior pattern, which involves the following roles:

First, abstract processor (Handler) Roles,Defines an interface for processing requests, and if necessary, the interface can define a method to return a reference to the home. A schematic class diagram is given:


Figure2, abstract processor role.

The accumulation relationship in the graph gives a reference to the other class, the abstract methodHandleRequest ()The operation that the subclass handles the request is normalized.

Second, the specific processor (Concretehandler) Roles,After processing the request, you can choose to dispose of the request or pass the request to the other. A schematic class diagram is given.


Figure3, and the specific processor role.


In the schematic of the specific processorConcretehandlerclass has onlyHandleRequest ()a method.

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



Figure4, and the class diagram definition of the responsibility chain pattern.

Pure and impure chain of Duty mode

A pure chain of responsibility requires a specific handler object to select only one of two behaviors: one is to take responsibility, and the other is to push the responsibility to the next. A situation in which a specific processor object is not allowed to transmit responsibility after taking part of the responsibility.

In a pure chain of responsibility, a request must be accepted by a handler object, and in an impure chain of responsibility, a request can ultimately not be accepted by any of the accepted end objects.

The actual example of the pure responsibility chain model is difficult to find, and the examples we see are the implementation of the impure responsibility chain model. Some people think that the impure chain of responsibility is not the chain of responsibility at all, which may make sense, but in the actual system, the pure responsibility chain is difficult to find, if the chain of responsibility is not pure is not the responsibility chain model, then the chain of responsibility mode will not have much significance.

Responsibility chain mode requires that all objects on the chain inherit from a common parent class
  

Under what circumstances is the responsibility chain model used?

Use the responsibility chain model in the following situations:

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

First, when more than one handler object handles a request, and does not know in advance which handler object is processing a request. This handler object is dynamically determined.

Second, when the system wants to issue a request to one of several processor objects, it is not clear which handler object will handle the request.

Thirdly, when a collection of handler objects that process a request needs to be specified dynamically.

Advantages and disadvantages of using the responsibility chain model

The chain of responsibility model reduces the coupling between the object that issued the command and the object that handles the command, which allows multiple and one handler objects to decide which processor will eventually process the command, based on their own logic. In other words, the object that issued the command simply passes the command to the beginning of the chain structure, without knowing which node on the chain is processing the command.

Obviously, this means that the system is allowed more flexibility in processing the command. Which object ultimately processes a command can vary depending on which objects participate in the chain of responsibility, and where the objects are located on the chain of responsibility.

The realization of the responsibility chain model

The origin of the chain structure

It is worth noting that the chain of responsibility model does not create a chain of responsibility. The creation of the responsibility chain must be done with the rest of the system.

The chain of responsibility model reduces the coupling between the sending and receiving ends of the request, allowing multiple objects to have the opportunity to process the request. A chain can be a line, a tree, or it can be a ring. The topological structure of a chain can be single-connected or multi-connected, and the responsibility chain pattern does not specify the topological structure of the chain of responsibility. But the chain of responsibility model requires that at the same time, orders can only be passed to a house (or be disposed of) and not to more than one. In the diagram below, the chain of responsibility is part of a tree structure.



Figure9, the chain of responsibility is part of the system's existing tree structure. The shadowed object in the figure gives a possible command propagation path.

The members of the chain of responsibility are often part of a larger structure. For example, in the previous discussion of the "dream of Red Mansions" in the Drums of flowersGame, all the members are members of the Jia House. If the members of the chain of responsibility do not exist, they must be created in order to use the responsibility chain pattern; The specific processor object of the chain of responsibility can be an instance of the same specific processor class.

InJavaOf1.0version ofAWTEvent HandlingModel, the chain of responsibility is the container hierarchy of components on the window.

In the following talks.Internet Explorerof theDHTMLof theDOMin the event-handling model, the chain of responsibility isDOMHierarchy itself.

Delivery of commands

There may not be a single command, but a number of commands, to pass on a chain of responsibility. These commands can take the form of a polymorphic implementation of the abstraction layer and materialization layer, thus separating the command object from the responsibility of the object on the chain of responsibility and separating the command object from the object propagating the command.



FigureTen, multiple commands are propagated on the chain of responsibility.

Example:

Filterchain manage the entire filterand inherit the filter yourself so you can attach additional filterchain

public class Filterchain implements Filter {list<filter> filters = new arraylist<filter> ();p rivate 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) {R Equest.setrequeststr (Request.getrequeststr (). Replace (' < ', ' [')]; Request.setrequeststr (REQUEST.GETREQUESTSTR ( ). Replace (' > ', '] ') + "---htmlfilter ()"), Chain.dofilter (request, response, chain); Response.setresponsestr ( RESPONSE.GETRESPONSESTR () + "---htmlfilter ()");} }


This paper draws on.

Source: Yesky Editor: Ark

Copyright NOTICE: Welcome reprint, Hope in your reprint at the same time, add the original address, thank you with

The responsibility chain model of Java pattern development

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.