设计模式之过滤器模式 发表于 2017-11-25 | 分类于 java | 过滤器模式 123456package test;import java.util.List;public interface Filter { public List<Computer> filter(List<Computer> regulars);} 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364package test;public class Computer { private Brand brand; private Type type; private int price; public Computer(Brand brand, Type type, int price) { this.brand = brand; this.type = type; this.price = price; } public Brand getBrand() { return brand; } public void setBrand(Brand brand) { this.brand = brand; } public Type getType() { return type; } public void setType(Type type) { this.type = type; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Computer [brand=" + brand + ", type=" + type + ", price=" + price + "]"; }}enum Brand { Apple("apple"), HUAWEI("hauwei"), Haier("haier"), DELL("dell"), HP("hp"); private String brand; Brand(String brand) { this.brand = brand; } public String getBrand() { return brand; }}enum Type { NOTEBOOK("notebook"), DESKTOP("desktop"); private String type; Type(String type) { this.type = type; } public String getType() { return type; }} 1234567891011121314151617181920package test;import java.util.ArrayList;import java.util.List;public class BrandFilter implements Filter { private Brand brand; public BrandFilter(Brand brand){ this.brand = brand; } @Override public List<Computer> filter(List<Computer> regulars) { // TODO Auto-generated method stub List<Computer> result = new ArrayList<>(); for(Computer regular:regulars){ if(regular.getBrand().equals(brand)){ result.add(regular); } } return result; }} 12345678910111213141516171819202122package test;import java.util.ArrayList;import java.util.List;public class TypeFilter implements Filter { private Type type; public TypeFilter(Type type) { this.type = type; } @Override public List<Computer> filter(List<Computer> regulars) { List<Computer> result = new ArrayList<>(); for(Computer regular:regulars){ if(regular.getType().equals(type)){ result.add(regular); } } return result; }} 12345678910111213141516171819202122package test;import java.util.ArrayList;import java.util.List;public class PriceFilter implements Filter { private int price; public PriceFilter(int price) { this.price = price; } @Override public List<Computer> filter(List<Computer> regulars) { List<Computer> result = new ArrayList<>(); for(Computer regular:regulars){ if(regular.getPrice() == price){ result.add(regular); } } return result; }} 12345678910111213package test;import java.util.ArrayList;import java.util.List;public class FilterUtils{ public static List<Computer> filter(List<Computer> regulars ,List<Filter> filters) { // TODO Auto-generated method stub List<Computer> result = new ArrayList<>(regulars); for(Filter filter: filters){ result =filter.filter(result); } return result; }} 123456789101112131415161718192021222324package test;import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class Test { public static void main(String[] args) { List<Computer> regulars = new ArrayList<>(); regulars.add(new Computer(Brand.Apple,Type.NOTEBOOK,8000)); regulars.add(new Computer(Brand.Apple,Type.NOTEBOOK,8000)); regulars.add(new Computer(Brand.DELL,Type.DESKTOP,3000)); regulars.add(new Computer(Brand.HP,Type.NOTEBOOK,6000)); regulars.add(new Computer(Brand.DELL,Type.NOTEBOOK,8000)); regulars.add(new Computer(Brand.Haier,Type.DESKTOP,6000)); regulars.add(new Computer(Brand.HUAWEI,Type.NOTEBOOK,8000)); System.out.println("库存信息"); System.out.println(Arrays.toString(regulars.toArray())); List<Filter> filters = new ArrayList<>(); filters.add(new BrandFilter(Brand.Apple)); filters.add(new TypeFilter(Type.NOTEBOOK)); List<Computer> result = FilterUtils.filter(regulars, filters); System.out.println("筛选后的信息"); System.out.println(Arrays.toString(result.toArray())); } } 如果对您有价值,看官可以打赏的!