过滤器链模式
类结构图
编辑
过滤器链
public class LabItemFilterChain { private List<LabItemFilter> labItemFilters; int pos = 0; public LabItemFilterChain() { this.labItemFilters = new LinkedList<>(); } public void addFilter(LabItemFilter labItemFilter) { labItemFilters.add(labItemFilter); } public LabItemRange doFilter(Report request,List<LabItemRange> response) { LabItemRange range = null; if (request==null|| CollectionUtils.isEmpty(response)) { return null; } if (pos<labItemFilters.size()) { range = labItemFilters.get(pos++).doFilter(request, response, this); if (range!=null) { return range; } } return null; } }
过滤器接口及默认比较方法
public interface LabItemFilter { LabItemRange doFilter(Report report, List<LabItemRange> itemRanges,LabItemFilterChain filterChain); default boolean compareCode(LabItemRange itemRange,Report report){ if (itemRange.getSampleCode()==null||report.getSampleCode()==null) { return false; } return Objects.equals(itemRange.getSampleCode(),report.getSampleCode()); } default boolean compareAge(LabItemRange itemRange,Report report){ Integer age = report.getAge(); Integer ageUnit = report.getAgeUnit(); Integer lowAge = itemRange.getLowAge(); Integer highAge = itemRange.getHighAge(); Integer lowAgeUnit = itemRange.getLowAgeUnit(); Integer highAgeUnit = itemRange.getHighAgeUnit(); if (age==null||ageUnit==null||lowAge==null||highAge==null||lowAgeUnit==null||highAgeUnit==null) { return false; } long ageSecond = age * AlisDateUtils.getSecondsByAgeUnit(ageUnit); long lowAgeSecond = lowAge * AlisDateUtils.getSecondsByAgeUnit(lowAgeUnit); long hignAgeSecond = highAge * AlisDateUtils.getSecondsByAgeUnit(highAgeUnit); return ageSecond>=lowAgeSecond&&ageSecond<=hignAgeSecond; } default boolean compareSex(LabItemRange itemRange,Report report){ if (itemRange.getSex()==null||report.getSex()==null) { return false; } return Objects.equals(itemRange.getSex(),report.getSex()); } }
接口的各种实现
public class LabItemByAgeFilter implements LabItemFilter{ @Override public LabItemRange doFilter(Report report, List<LabItemRange> itemRanges, LabItemFilterChain filterChain) { for (LabItemRange itemRange : itemRanges) { if (Objects.isNull(itemRange.getSampleCode())&&Objects.isNull(itemRange.getSex())&&compareAge(itemRange,report)) { return itemRange; } } return filterChain.doFilter(report, itemRanges); } }
public class LabItemByCodeAgeFilter implements LabItemFilter{ @Override public LabItemRange doFilter(Report report, List<LabItemRange> itemRanges, LabItemFilterChain filterChain) { for (LabItemRange itemRange : itemRanges) { if (Objects.isNull(itemRange.getSex())&&compareCode(itemRange,report)&&compareAge(itemRange,report)) { return itemRange; } } return filterChain.doFilter(report, itemRanges); } }
调用
LabItemFilterChain chain = new LabItemFilterChain(); chain.addFilter(new LabItemByCodeAgeSexFilter()); chain.addFilter(new LabItemByCodeAgeFilter()); chain.addFilter(new LabItemByCodeSexFilter()); chain.addFilter(new LabItemByCodeFilter()); chain.addFilter(new LabItemByAgeSexFilter()); chain.addFilter(new LabItemByAgeFilter()); chain.addFilter(new LabItemBySexFilter()); chain.addFilter(new LabItemDefaultFilter()); LabItemRange labItemRange = chain.doFilter(report, labItemRangeList); return labItemRange;
匿名方法实现
编辑