Luckylau's Blog

设计模式之过滤器模式

过滤器模式

1
2
3
4
5
6
package test;
import java.util.List;
public interface Filter {
public List<Computer> filter(List<Computer> regulars);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package 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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package 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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package 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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package 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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
package 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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package 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()));
}
}
Luckylau wechat
如果对您有价值,看官可以打赏的!