Design Patterns - SPLessons
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. Filter.java [java]public interface Filter { public void execute(String request); }[/java] Here just created the Filter interface. AuthenticationFilter.java [java]public class AuthenticationFilter implements Filter { public void execute(String request){ System.out.println("Authenticating request: " + request); } }[/java] Here created the class AuthenticationFilter that is implementing Filter and Created concrete filters. DebugFilter.java [java]public class DebugFilter implements Filter { public void execute(String request){ System.out.println("request log: " + request); } }[/java] Here created the another class DebugFilters that is implementing Filter. Target.java [java]public class Target { public void execute(String request){ System.out.println("Executing request: " + request); } }[/java] Here created the target where request is passing that mean when compile the code given input will be displayed. FilterChain.java [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; } }[/java] 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 [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); } }[/java] Here just created the Filter Manager. Client.java [java]public class Client { FilterManager filterManager; public void setFilterManager(FilterManager filterManager){ this.filterManager = filterManager; } public void sendRequest(String request){ filterManager.filterRequest(request); } }[/java] This Client class is used to demonstrate Intercepting Filter Design Pattern. InterceptingFilterDemo.java [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"); } }[/java] Output Now compile the code result will be as follows. [java] Authenticating request: SPLESSON request log: SPLESSON Executing request: SPLESSON[/java]

Summary

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.