Filter mode: This mode allows developers to use different standards to filter a set of objects and connect them through logical operations. This type of design pattern is a structural pattern, which combines multiple standards to obtain a single standard. [Rookie] In fact, a set of objects are screened according to conditions.
Article Catalog
Our car manufacturer now has a lot of models, and now I just want to screen out the number of BWM.
package filter;
/************************************************
*@ClassName : Car
*@Description : TODO
*@Author : NikolaZhang
*@Date : 【2018/12/15 0015 17:21】
*@Version : 1.0.0
*************************************************/
public class Car {
private String name;
public String getName() {
return name;
}
public Car(String name) {
this.name = name;
}
}
We use all the car as a parameter, and screen BWM.
package filter;
import java.util.ArrayList;
import java.util.List;
/************************************************
*@ClassName : CarFilter
*@Description : TODO
*@Author : NikolaZhang
*@Date : 【2018/12/15 0015 17:20】
*@Version : 1.0.0
*************************************************/
public class CarFilter {
private List<Car> list = new ArrayList<>();
public List<Car> filterCar(List<Car> listCar){
for (Car car :listCar){
if("bwm".equals(car.getName())){
list.add(car);
}
}
return list;
}
}
package filter;
import java.util.ArrayList;
import java.util.List;
/************************************************
*@ClassName : Test
*@Description : TODO
*@Author : NikolaZhang
*@Date : 【2018/12/15 0015 17:20】
*@Version : 1.0.0
*************************************************/
public class Test {
public static void main(String[] args) {
List<Car> list = new ArrayList<>();
list.add(new Car("bwm"));
list.add(new Car("benz"));
list.add(new Car("BWM"));
CarFilter carFilter = new CarFilter();
System.out.println("The number of eligible cars:"+carFilter.filterCar(list).size());
}
}
[Rookie Tutorial]:http://www.runoob.com/design-pattern/filter-pattern.html