今天带给大家的是过滤器模式,这个模式通俗易懂,我们直切正题!
过滤器模式是一种结构型模式,这类模式更关注类和对象的组合。啥是过滤器模式呢,比如现在有一个动物类,这个动物类里面有猫、狗、狮子、猴子等等,那么你要怎么从里面过滤出狗的对象呢?你要怎么从里面过滤出即是公并且是狮子的对象呢?
我们称一种过滤条件为一种标准,基于各种标准去过滤出你想要的对象列表,这一系列动作通过代码的方式实现解耦,我们叫做过滤器模式。
现在举一个例子,现在有一个电脑类,这个电脑由很多零件组装而成,比如说cpu,内存条等等,我们现在按照我们想要的标准去过滤符合条件的电脑。
类图:
具体的代码实现:
public class Computer {
// cpu型号
private String cpuType;
// 内存大小
private int memorySize;
// 操作系统类型
private String operatingSystemType;
public Computer(String cpuType, int memorySize, String operatingSystemType) {
this.cpuType = cpuType;
this.memorySize = memorySize;
this.operatingSystemType = operatingSystemType;
}
public String getCpuType() {
return cpuType;
}
public void setCpuType(String cpuType) {
this.cpuType = cpuType;
}
public int getMemorySize() {
return memorySize;
}
public void setMemorySize(int memorySize) {
this.memorySize = memorySize;
}
public String getOperatingSystemType() {
return operatingSystemType;
}
public void setOperatingSystemType(String operatingSystemType) {
this.operatingSystemType = operatingSystemType;
}
@Override
public String toString() {
return "Computer{" +
"cpuType='" + cpuType + '\'' +
", memorySize=" + memorySize +
", operatingSystemType='" + operatingSystemType + '\'' +
'}';
}
}
public interface Criteria {
public List<Computer> meetCriteria(List<Computer> computerList);
}
public class CriteriaI5 implements Criteria {
@Override
public List<Computer> meetCriteria(List<Computer> computerList) {
List<Computer> i5Computer = new ArrayList<>();
for(Computer computer : computerList) {
if(computer.getCpuType().equals("I5")) {
i5Computer.add(computer);
}
}
return i5Computer;
}
}
public class Criteria8G implements Criteria {
@Override
public List<Computer> meetCriteria(List<Computer> computerList) {
List<Computer> _8gComputer = new ArrayList<>();
for(Computer computer : computerList) {
if(computer.getMemorySize() == 8 * 1024) {
_8gComputer.add(computer);
}
}
return _8gComputer;
}
}
public class AndCriteria implements Criteria {
private Criteria criteria;
private Criteria otherCriteria;
public AndCriteria(Criteria criteria, Criteria otherCriteria) {
this.criteria = criteria;
this.otherCriteria = otherCriteria;
}
@Override
public List<Computer> meetCriteria(List<Computer> computerList) {
List<Computer> computers = criteria.meetCriteria(computerList);
return otherCriteria.meetCriteria(computers);
}
}
public class OrCriteria implements Criteria {
private Criteria criteria;
private Criteria otherCriteria;
public OrCriteria(Criteria criteria, Criteria otherCriteria) {
this.criteria = criteria;
this.otherCriteria = otherCriteria;
}
@Override
public List<Computer> meetCriteria(List<Computer> computerList) {
List<Computer> computers1 = criteria.meetCriteria(computerList);
List<Computer> computers2 = otherCriteria.meetCriteria(computerList);
for(Computer computer : computers2) {
if(!computers1.contains(computer)) {
computers1.add(computer);
}
}
return computers1;
}
}
public class Client {
public static void main(String[] args) {
List<Computer> computerList = new ArrayList<>();
computerList.add(new Computer("I5", 1024, "Windows"));
computerList.add(new Computer("I5", 1024 * 8, "Linux"));
computerList.add(new Computer("I7", 1024 * 8, "Windows"));
computerList.add(new Computer("I7", 1024, "Linux"));
computerList.add(new Computer("I3", 1024 * 4, "Linux"));
Criteria criteriaI5 = new CriteriaI5();
Criteria criteria8G = new Criteria8G();
Criteria criteriaI5Or8G = new OrCriteria(criteriaI5, criteria8G);
Criteria criteriaI5And8G = new AndCriteria(criteriaI5, criteria8G);
System.out.println("cpuType = I5----------------------------");
printComputer(criteriaI5.meetCriteria(computerList));
System.out.println("memorySize = 8G-------------------------");
printComputer(criteria8G.meetCriteria(computerList));
System.out.println("cpuType = I5 or memorySize = 8G---------");
printComputer(criteriaI5Or8G.meetCriteria(computerList));
System.out.println("cpuType = I5 and memorySize = 8G--------");
printComputer(criteriaI5And8G.meetCriteria(computerList));
}
public static void printComputer(List<Computer> computers) {
for(Computer computer : computers) {
System.out.println(computer);
}
}
}
/*
cpuType = I5----------------------------
Computer{cpuType='I5', memorySize=1024, operatingSystemType='Windows'}
Computer{cpuType='I5', memorySize=8192, operatingSystemType='Linux'}
memorySize = 8G-------------------------
Computer{cpuType='I5', memorySize=8192, operatingSystemType='Linux'}
Computer{cpuType='I7', memorySize=8192, operatingSystemType='Windows'}
cpuType = I5 or memorySize = 8G---------
Computer{cpuType='I5', memorySize=1024, operatingSystemType='Windows'}
Computer{cpuType='I5', memorySize=8192, operatingSystemType='Linux'}
Computer{cpuType='I7', memorySize=8192, operatingSystemType='Windows'}
cpuType = I5 and memorySize = 8G--------
Computer{cpuType='I5', memorySize=8192, operatingSystemType='Linux'}
*/
复制代码
好啦,本期如此简单,相信你一看就会!
愿每个人都能带着怀疑的态度去阅读文章并探究其中原理。
道阻且长,往事作序,来日为章。
期待我们下一次相遇!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END