Design Patterns - SPLessons

Intercepting Pattern

Chapter 33

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Intercepting Pattern

Intercepting 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 Pattern 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

Intercepting 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.
Filter.java

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

Here just created the Filter interface.
AuthenticationFilter.java

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.
DebugFilter.java

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.
Target.java

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.
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;
   }
}

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.
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);
   }
}

Here just created the Filter Manager.
Client.java

public class Client {
   FilterManager filterManager;

   public void setFilterManager(FilterManager filterManager){
      this.filterManager = filterManager;
   }

   public void sendRequest(String request){
      filterManager.filterRequest(request);
   }
}

This Client class is used to demonstrate 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("SPLESSONS");
   }
}

Output
Now compile the code result will be as follows.

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


Summary

shape Key Points

  • Filter Pattern that allows developers to filter a group of objects using various criteria
  • Intercepting Pattern is utilized when somebody need to do some pre-preparing/post-handling with solicitation or reaction of the application.