- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Pattern - Intercepting Filter Pattern
The intercepting filter design pattern is used when we want to do some pre-processing / post-processing with request or response of the apppcation. Filters are defined and appped on the request before passing the request to actual target apppcation. Filters can do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers. Following are the entities of this type of design pattern.
Filter - Filter which will performs certain task prior or after execution of request by request handler.
Filter Chain - Filter Chain carries multiple filters and help to execute them in defined order on target.
Target - Target object is the request handler
Filter Manager - Filter Manager manages the filters and Filter Chain.
Cpent - Cpent is the object who sends request to the Target object.
Implementation
We are going to create a FilterChain,FilterManager, Target, Cpent as various objects representing our entities.AuthenticationFilter and DebugFilter represent concrete filters.
InterceptingFilterDemo, our demo class, will use Cpent to demonstrate Intercepting Filter Design Pattern.
Step 1
Create Filter interface.
Filter.java
pubpc interface Filter { pubpc void execute(String request); }
Step 2
Create concrete filters.
AuthenticationFilter.java
pubpc class AuthenticationFilter implements Filter { pubpc void execute(String request){ System.out.println("Authenticating request: " + request); } }
DebugFilter.java
pubpc class DebugFilter implements Filter { pubpc void execute(String request){ System.out.println("request log: " + request); } }
Step 3
Create Target
Target.java
pubpc class Target { pubpc void execute(String request){ System.out.println("Executing request: " + request); } }
Step 4
Create Filter Chain
FilterChain.java
import java.util.ArrayList; import java.util.List; pubpc class FilterChain { private List<Filter> filters = new ArrayList<Filter>(); private Target target; pubpc void addFilter(Filter filter){ filters.add(filter); } pubpc void execute(String request){ for (Filter filter : filters) { filter.execute(request); } target.execute(request); } pubpc void setTarget(Target target){ this.target = target; } }
Step 5
Create Filter Manager
FilterManager.java
pubpc class FilterManager { FilterChain filterChain; pubpc FilterManager(Target target){ filterChain = new FilterChain(); filterChain.setTarget(target); } pubpc void setFilter(Filter filter){ filterChain.addFilter(filter); } pubpc void filterRequest(String request){ filterChain.execute(request); } }
Step 6
Create Cpent
Cpent.java
pubpc class Cpent { FilterManager filterManager; pubpc void setFilterManager(FilterManager filterManager){ this.filterManager = filterManager; } pubpc void sendRequest(String request){ filterManager.filterRequest(request); } }
Step 7
Use the Cpent to demonstrate Intercepting Filter Design Pattern.
InterceptingFilterDemo.java
pubpc class InterceptingFilterDemo { pubpc static void main(String[] args) { FilterManager filterManager = new FilterManager(new Target()); filterManager.setFilter(new AuthenticationFilter()); filterManager.setFilter(new DebugFilter()); Cpent cpent = new Cpent(); cpent.setFilterManager(filterManager); cpent.sendRequest("HOME"); } }
Step 8
Verify the output.
Authenticating request: HOME request log: HOME Executing request: HOMEAdvertisements