Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of java responsibility chain Model

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of java responsibility chain model, which is very detailed and has certain reference value. Friends who are interested must finish it!

Overview

In real life, there are often cases in which multiple objects can be processed in a request, but each object has different processing conditions or permissions. For example, when employees of a company ask for leave, leaders who can grant leave include department heads, deputy general managers, general managers, etc., but the number of days that each leader can approve is different, and employees must find different leaders to sign according to the number of days they want to take time off. in other words, employees must remember the name, phone number and address of each leader, which makes it more difficult for employees to ask for leave. Because there are so many leaders, employees have to judge which leader they are looking for, so it will be particularly troublesome. There are many such examples, such as finding leaders for travel reimbursement, "drumming and spreading flowers" games in life, and so on.

Said so much, I do not know if you have ever asked for leave in the company, if you have asked for leave, think about whether this is the case ah! Obviously, in this example, asking for leave is a request, and multiple objects can handle the request, such as department head, deputy general manager, general manager, etc., they can all grant leave, but the processing conditions or permissions of each object are different. for example, the head of the department may only grant one or two days of leave, and once the number of days of leave exceeds, the employee will have to go to the immediate boss of the head of the department. That is, the deputy general manager, if it also exceeds the scope of the leave granted by the deputy general manager, then the staff will have to go to the general manager to approve the leave, does this increase the difficulty of asking for leave?

Now that the problem has emerged, how to solve it? Use the chain of responsibility mode. So what is the chain of responsibility model? Let's take a look at its concept.

Also known as the responsibility chain pattern, in order to avoid the request sender being coupled with multiple request handlers, all request handlers are connected into a chain by remembering the reference of their next object through the previous object; when a request occurs, the request can be passed along this chain until an object processes it.

Many people read it and have no idea what it means. Here I will explain it to you a little bit. In the case of employee leave, the sender refers to the employee, because it is the employee (such as Zhang San) who asks for leave; multiple request handlers refer to the department head, deputy general manager, general manager, and so on. In this way, the schematic diagram of Zhang San's request for leave is as follows.

As can be seen from the picture above, if Zhang San wants to ask for leave, then he only needs to go to the head of his own department, because for him, he must know who is in charge of his department. Then, the head of the department will decide whether to grant leave according to the number of days Zhang San asks for leave. If the head of the department can grant leave, then he will naturally help Zhang San; but if he cannot approve it, then he will go to his immediate boss, that is, the deputy general manager, because they are already linked together. By the same token, the deputy general manager is the same, and he will judge according to the number of days of leave he can approve. If it is within the scope of his approval, then he will not say much nonsense and grant leave directly; if it cannot be approved, then go to his immediate boss, that is, the general manager. In this way, when the whole chain is finished, Zhang San's process of asking for leave is over.

After understanding the concept of the chain of responsibility model, let's take a look at the structure of the chain of responsibility pattern.

structure

The chain of responsibility model mainly includes the following roles:

Abstract handler (Handler) role: defines an interface for handling requests, including abstract processing methods and a successor connection (that is, remembering a reference to the next object).

Note that for this role, we can define it as either an interface or an abstract class, which is generally defined as an abstract class.

Concrete handler (Concrete Handler) role: implement the abstract processor's processing method to determine whether the request can be processed, if it can be processed, then process it, otherwise transfer the request to its successor.

The Client role: creates a processing chain and submits a request to the specific handler object at the head of the chain, which does not care about the processing details and the delivery of the request.

In other words, the client class does not need to find the corresponding object to deal with, but only needs to create the processing chain. Take the above schematic diagram of Zhang San's request for leave, for example, he only needs to find his own department head, and he does not need to pay attention to the steps of the leave process.

The above is all the content of this article "sample Analysis of java chain of responsibility Model". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report