Detailed explanation of responsibility chain mode of Java design mode

The beginning of Dr. Yan Hong's Book Java and mode describes the chain of responsibility mode as follows:

Responsibility chain model is a behavior model of objects. In the responsibility chain model, many objects are connected by each object's reference to its next family to form a chain. Requests are passed along the chain until an object in the chain decides to process the request. The client issuing the request does not know which object in the chain will eventually process the request, which makes the system dynamically reorganize and allocate responsibilities without affecting the client.

Talking from beating drums to passing flowers

Beating drums and passing flowers is a lively and tense drinking game. At the banquet, the guests sit down in turn and play the drum by one person. The place where the drum is played and the place where the flowers are passed are separated to show justice. When you start beating the drum, the bouquet begins to pass in turn. As soon as the drum falls, if the bouquet is in someone's hand, that person has to drink.

For example, Jia Mu, Jia amnesty, Jia Zheng, Jia Baoyu and Jia Huan are five flower passers who participate in the drum flower passing game. They form a chain. The drummer passed the flowers to Jia Mu and began the flower passing game. The flowers are passed from Jia's mother to Jia amnesty, from Jia amnesty to Jia Zheng, from Jia Zheng to Jia Baoyu, from Jia Baoyu to Jia Huan, and from Jia Huan back to Jia's mother, as shown in the figure below. When the drum stops, the person with flowers in his hand has to carry out the drinking order.

Beating drums and passing flowers is the application of the responsibility chain model. The responsibility chain may be a straight line, a ring chain or part of a tree structure.

Structure of responsibility chain model

The following uses the simplest implementation of a responsibility chain pattern.

The roles involved in the responsibility chain model are as follows:

● Abstract handler role: define an interface for processing requests. If necessary, the interface can define a method to set and return a reference to the next home. This role is usually implemented by a Java abstract class or Java interface. In the figure above, the aggregation relationship of the handler class gives the reference of the specific subclass to the next family, and the abstract method handlerequest () regulates the subclass's operation of processing requests. ● concrete handler role: after receiving the request, the specific handler can choose to dispose of the request or send the request to the next home. Since the specific handler holds a reference to the next home, the specific handler can access the next home if necessary.

source code

Abstract handler role

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>