Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Intercepting Filter Pattern

Intercepting Filter Pattern

The Intercepting Filter Pattern (Intercepting Filter Pattern) is used to perform some pre-processing/post-processing of an application's requests or responses. Filters are defined and applied to the request before passing it to the actual target application. Filters can perform authentication/authorization, logging, or request tracking, and then pass the request to the appropriate handler. The following are the entities of this design pattern.


Summary

Intent

Used to pre-process and post-process requests through a series of filters before the request reaches its final destination.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

  1. Separation of responsibilities: Separates common operations from business logic, improving code maintainability.
  2. Extensibility: Filters can be flexibly added, removed, or modified.
  3. Reusability: Filters can be reused across different request processing.

Disadvantages

Usage recommendation

Notes

Main roles included

  1. Filter Interface (Filter Interface):

  2. Defines the methods a filter must implement.
  3. - Defines the methods a filter must implement.
    1. Concrete Filter (Concrete Filter):

    2. Implements the filter interface and contains specific processing logic.
    3. - Implements the filter interface and contains specific processing logic.
      1. Filter Manager (Filter Manager):

      2. Responsible for maintaining the filter chain and invoking filters.
      3. - Responsible for maintaining the filter chain and invoking filters.
        1. Target Object (Target):

        2. The final destination of request processing, the implementation of business logic.
        3. - The final destination of request processing, the implementation of business logic.
          1. Client (optional):

          2. The web browser or API client that issues the request.
          3. - The web browser or API client that issues the request.

            The Intercepting Filter pattern helps keep business logic clear and focused by inserting common operations into the request processing flow.


            Create a class that implements

            We will create FilterChain、FilterManager、Target、Client As the various objects representing entities.AuthenticationFilter and DebugFilter Represent entity filters.

            InterceptingFilterDemo Class uses Client To demonstrate the Intercepting Filter design pattern.

            Step 1

            Create the filter interface Filter.

            Filter.java

            public

            interface

            Filter

            {

            public

            void

            execute

            (

            String

            request

            )

            ;

            }

            Step 2

            Create the entity filters.

            AuthenticationFilter.java

            public

            class

            AuthenticationFilter

            implements

            Filter

            {

            public

            void

            execute

            (

            String

            request

            )

            {

            System

            .

            out

            .

            println

            (

            "

            Authenticating request:

            "

            request

            )

            ;

            }

            }

            DebugFilter.java

            public

            class

            DebugFilter

            implements

            Filter

            {

            public

            void

            execute

            (

            String

            request

            )

            {

            System

            .

            out

            .

            println

            (

            "

            request log:

            "

            request

            )

            ;

            }

            }

            Step 3

            Create the Target.

            Target.java

            public

            class

            Target

            {

            public

            void

            execute

            (

            String

            request

            )

            {

            System

            .

            out

            .

            println

            (

            "

            Executing request:

            "

            request

            )

            ;

            }

            }

            Step 4

            Create the filter chain.

            FilterChain.java

            import

            java

            .

            util

            .

            ArrayList

            ;

            import

            java

            .

            util

            .

            List

            ;

            public

            class

            FilterChain

            {

            private

            List

            <
            
            Filter
            
            filters
            
            =
            
            new
            
            ArrayList
            
            <
            
            Filter
            
            (
            
            )

            ;

            private

            Target

            target

            ;

            public

            void

            addFilter

            (

            Filter

            filter

            )

            {

            filters

            .

            add

            (

            filter

            )

            ;

            }

            public

            void

            execute

            (

            String

            request

            )

            {

            for

            (

            Filter

            filter

            :

            filters

            )

            {

            filter

            .

            execute

            (

            request

            )

            ;

            }

            target

            .

            execute

            (

            request

            )

            ;

            }

            public

            void

            setTarget

            (

            Target

            target

            )

            {

            this

            .

            target

            =

            target

            ;

            }

            }

            Step 5

            Create the filter manager.

            FilterManager.java

            public

            class

            FilterManager

            {

            FilterChain

            filterChain

            ;

            public

            FilterManager

            (

            Target

            target

            )

            {

            filterChain

            =

            new

            FilterChain

            (

            )

            ;

            filterChain

            .

            setTarget

            (

            target

            )

            ;

            }

            public

            void

            setFilter

            (

            Filter

            filter

            )

            {

            filterChain

            .

            addFilter

            (

            filter

            )

            ;

            }

            public

            void

            filterRequest

            (

            String

            request

            )

            {

            filterChain

            .

            execute

            (

            request

            )

            ;

            }

            }

            Step 6

            Create the client Client.

            Client.java

            public

            class

            Client

            {

            FilterManager

            filterManager

            ;

            public

            void

            setFilterManager

            (

            FilterManager

            filterManager

            )

            {

            this

            .

            filterManager

            =

            filterManager

            ;

            }

            public

            void

            sendRequest

            (

            String

            request

            )

            {

            filterManager

            .

            filterRequest

            (

            request

            )

            ;

            }

            }

            Step 7

            Use Client To demonstrate the Intercepting Filter design pattern.

            InterceptingFilterDemo.java

            public

            class

            InterceptingFilterDemo

            {

            public

            static

            void

            main

            (

            String

            [

            ]

            args

            )

            {

            FilterManager

            filterManager

            =

            new

            FilterManager

            (

            new

            Target

            (

            )

            )

            ;

            filterManager

            .

            setFilter

            (

            new

            AuthenticationFilter

            (

            )

            )

            ;

            filterManager

            .

            setFilter

            (

            new

            DebugFilter

            (

            )

            )

            ;

            Client

            client

            =

            new

            Client

            (

            )

            ;

            client

            .

            setFilterManager

            (

            filterManager

            )

            ;

            client

            .

            sendRequest

            (

            "

            HOME

            "

            )

            ;

            }

            }

            Step 8

            Run the program, output result:

            
            
            Authenticating request: HOME
            
            request log: HOME
            
            Executing request: HOME