"Design mode" Interceptor Filter mode

Source: Internet
Author: User

The Interceptor filter pattern (intercepting filter pattern) is used to preprocess/post-process the application's request or response. Define the filter and apply it to the request before passing it on to the actual target application. The filter can do authentication/authorization/logging, or track the request, and then pass the request to the appropriate handler. The following are the entities for this design pattern.

    • Filter -The filter performs certain tasks before or after the request handler executes the request.
    • The filter chain (filter Chain) -the filter chain has multiple filters and executes the filters in the order defined on Target.
    • The target -target object is the request handler.
    • Filter Manager-Filter Manager manages filters and filter chains.
    • clients-Client is the object that sends the request to the target object.
Realize

We will create Filterchain,filtermanager,Target, andClient as various objects representing the entity. authenticationfilter and debugfilter represent entity filters.

Interceptingfilterdemo, our demo class uses the Client to demonstrate the Interceptor filter design pattern.

Step 1

Create a filter interface filter.

 Public Interface Filter {   publicvoid  execute (String request);}

Step 2

Create an entity filter.

 Public class Implements Filter {   publicvoid  execute (String request) {      System.out.println ( "Authenticating Request:" + request);}   }
 Public class Implements Filter {   publicvoid  execute (String request) {      System.out.println (" Request log: "+ request";   }}
Step 3

Create Target.

 Public class Target {   publicvoid  execute (String request) {      System.out.println ( "Executing request:" + request);}   }
Step 4

Create a filter chain.

Importjava.util.ArrayList;Importjava.util.List; Public classFilterchain {Privatelist<filter> filters =NewArraylist<filter>(); Privatetarget target;  Public voidAddFilter (filter filter) {Filters.add (filter); }    Public voidExecute (String request) { for(Filter filter:filters) {filter.execute (request);   } target.execute (Request); }    Public voidsettarget (target target) { This. target =Target; }}
Step 5

Create a filter manager.

 Public class Filtermanager {   filterchain filterchain;     Public Filtermanager (target target) {      new  filterchain ();      Filterchain.settarget (target);   }     Public void SetFilter (filter filter) {      filterchain.addfilter (filter);   }     Public void filterrequest (String request) {      filterchain.execute (request);}   }
Step 6

Create client clients.

 Public class Client {   filtermanager filtermanager;     Public void Setfiltermanager (Filtermanager filtermanager) {      this. Filtermanager= filtermanager;   }     Public void SendRequest (String request) {      filtermanager.filterrequest (request);}   }
Step 7

Use the Client to demonstrate the Interceptor filter design pattern.

 Public classInterceptingfilterdemo { Public Static voidMain (string[] args) {Filtermanager Filtermanager=NewFiltermanager (NewTarget ()); Filtermanager.setfilter (NewAuthenticationfilter ()); Filtermanager.setfilter (NewDebugfilter ()); Client Client=NewClient ();      Client.setfiltermanager (Filtermanager); Client.sendrequest ("HOME"); }}
Step 8

Verify the output.

Authenticating Request:homerequest log:homeexecuting Request:home

"Design mode" Interceptor Filter mode

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.