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

How to realize the responsibility chain in Java Design pattern

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to realize the chain of responsibility in Java design pattern". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The goal of the chain of responsibility pattern (Chain of Responsibility) is to give multiple objects the opportunity to process the request, thus avoiding the coupling between the sender and receiver of the request. Join these objects into a chain and pass the request along the chain until an object processes it.

When dealing with a user's request, you may have to add different processing logic to the request according to different circumstances, at this time you can use the responsibility chain to design. When you need to add a processing logic, you can easily add a processing node.

Now our requirement is to process the user's request, to process the string information submitted by the user layer by layer, and to process the returned string layer by layer when the result is returned after the processing is completed. when dealing with the returned case, the order of processing is exactly the opposite of the previous order.

First, the user's request and receiving objects Request and Response are established:

Package com.lcq.filter; public class Request {String requestStr; public String getRequestStr () {return requestStr;} public void setRequestStr (String requestStr) {this.requestStr = requestStr;}} package com.lcq.filter; public class Response {String responseStr; public String getResponseStr () {return responseStr } public void setResponseStr (String responseStr) {this.responseStr = responseStr;}}

We abstract the logic of processing user information into individual filters, and further abstract the filter interface Filter:

Package com.lcq.filter; public interface Filter {public void doFilter (Request request, Response response,FilterChain chain);}

Notice that there is a variable of FilterChain in the doFilter method parameter in the Filte interface, and then we create the FilterChain class:

Package com.lcq.filter; import java.util.ArrayList; import java.util.List; public class FilterChain implements Filter {List filters = new ArrayList (); int index = 0; public FilterChain addFilter (Filter f) {this.filters.add (f); return this @ Override 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);}}

The doFilter method is implemented by inheriting the Filter interface in FilterChain, and there is another index variable in FilterChain, which is used to mark which filter is currently accessed. These filters are stored in ArrayList, so that users can implement their own filters and write their own processing logic when using them, so that they can add their own filters to ArrayList, and then call FilterChain's doFilter method to traverse the entire chain of responsibility.

Let's write three filters:

HTMLFilter class:

Package com.lcq.filter; / * filter script elements in HTML * @ author lcq * * / public class HTMLFilter implements Filter {@ Override public void doFilter (Request request, Response response,FilterChain chain) {request.requestStr = request.getRequestStr (). Replace (","]-HTMLFilter "); chain.doFilter (request, response, chain) Response.responseStr + = "- HTMLFilter";}}

SesitiveFilter class:

Package com.lcq.filter; public class SesitiveFilter implements Filter {@ Override public void doFilter (Request request, Response response, FilterChain chain) {request.requestStr = request.getRequestStr () .replace ("sensitive", ") .replace (" cat "," -SesitiveFilter "); chain.doFilter (request, response, chain); response.responseStr + ="-SesitiveFilter " }}

FaceFilter class:

Package com.lcq.filter; public class FaceFilter implements Filter {@ Override public void doFilter (Request request, Response response, FilterChain chain) {request.requestStr = request.getRequestStr () .replace (":)", "^ V ^-FaceFilter"); chain.doFilter (request, response, chain); response.responseStr + = "- FaceFilter";}}

* write test classes:

Package com.lcq.filter; public class Main {public static void main (String [] args) {String message = "sensitive words, Chongqing, peekaboo:)"; Request request = new Request (); request.setRequestStr (message); Response response = new Response (); response.setResponseStr ("response"); FilterChain fc = new FilterChain () Fc.addFilter (new HTMLFilter ()) .addFilter (new SesitiveFilter ()); FilterChain fc2 = new FilterChain (); fc2.addFilter (new FaceFilter ()); fc.addFilter (fc2); fc.doFilter (request, response,fc); System.out.println ("request =" + request.getRequestStr ()); System.out.println ("response =" + response.getResponseStr ());}}

In the above example, you should pay attention to two things:

1. The FilterChain we built inherits the Filter interface, so we can use FilterChain in the test class just like other filters, which greatly improves the flexibility.

two。 For the problem of access processing order of the responsibility chain, the solution of this problem is the idea of recursion, so that the first called node calls the filter in the opposite order when dealing with the returned results. This solution is common and ingenious in implementing filters and interceptors in Struts and other frameworks.

This is the end of the content of "how to achieve the chain of responsibility in the Java design pattern". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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