31) intercepting filter design pattern
类别:
问题:
方案:
示例:
import java.util.ArrayList; import java.util.List; public class InterceptingFilterPatternDemo { 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"); } } interface Filter { public void execute(String request); } class AuthenticationFilter implements Filter { public void execute(String request) { System.out.println("filter Authenticating : " + request); } } class DebugFilter implements Filter { public void execute(String request) { System.out.println("filter Log: " + request); } } class Target { public void execute(String request) { System.out.println("target Executing : " + request); } } 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; } } 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); } } class Client { FilterManager filterManager; public void setFilterManager(FilterManager filterManager) { this.filterManager = filterManager; } public void sendRequest(String request) { filterManager.filterRequest(request); } }
filter Authenticating : home filter Log: home target Executing : home
应用:
不足:(
优化:)