Design Patterns - SPLessons

Filter Pattern

Chapter 12

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Filter Pattern

Filter Pattern

shape Description

Filter Pattern or Criteria example is a configuration example that empowers designers to channel an arrangement of items utilizing diverse criteria and affixing them in a decoupled route through legitimate operations. This sort of configuration example goes under auxiliary example as this example joins various criteria to get single criteria.
The intercepting filter example is utilized when the programmer need to do some pre-handling/post-preparing with solicitation or reaction of the application. Channels are characterized and connected on the solicitation before passing the solicitation to genuine target application. Channels can do the confirmation/approval/logging or following of solicitation and after that pass the solicitations to comparing handlers. Taking after are the elements of this kind of configuration example.

shape Example

Filter Pattern – Following is an example To make a FilterChain,FilterManager, Target, Client as different items speaking to our entities.AuthenticationFilter and DebugFilter speak to concrete channel.

public interface Filter {
   public void execute(String request);

Here just created the Filter interface.

public class AuthenticationFilter implements Filter {
   public void execute(String request){
      System.out.println("Authenticating request: " + request);

Here created the class AuthenticationFilter that is implementing Filter and Created concrete filters.

public class DebugFilter implements Filter {
   public void execute(String request){
      System.out.println("request log: " + request);

Here created the another class DebugFilters that is implementing Filter.

public class Target {
   public void execute(String request){
      System.out.println("Executing request: " + request);

Here created the target where request is passing that mean when compile the code given input will be displayed.

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){
   public void execute(String request){
      for (Filter filter : filters) {
   public void setTarget(Target target){ = target;

Here created the Filter chain class and used filter execute method and also setted the target, where this keywoed is used to reffer current object.

public class FilterManager {
   FilterChain filterChain;
   public FilterManager(Target target){
      filterChain = new FilterChain();
   public void setFilter(Filter filter){
   public void filterRequest(String request){

Here just created the Filter Manager.

public class Client {
   FilterManager filterManager;
   public void setFilterManager(FilterManager filterManager){
      this.filterManager = filterManager;
   public void sendRequest(String request){

This Client class is used to demonstrate Intercepting Filter Design Pattern.

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();

Now compile the code result will be as follows.

Authenticating request: SPLESSON
request log: SPLESSON
Executing request: SPLESSON


shape Key Points

  • Filter Pattern that allows developers to filter a group of objects using various criteria
  • Filter Pattern – Target object is the request handler.