提交 d69e9b51 编写于 作者: C chenjianqiang

增加过滤器模式 ○( ^皿^)っHiahiahia…○( ^皿^)っHiahiahia…

上级 3fcf9f8b
package com.pattern.FilterPattern.materials;
import com.pattern.FilterPattern.materials.filter.Filter;
import com.pattern.FilterPattern.materials.filter.PaperFilter;
import com.pattern.FilterPattern.materials.po.JunkDTO;
import java.util.ArrayList;
import java.util.List;
/**
* 过滤器模式
*
......@@ -7,7 +14,23 @@ package com.pattern.FilterPattern.materials;
* @date 2021/11/25 13:56
**/
public class FilterMain {
public static void main(String[] args) {
public void main() {
// 我是收破烂的小老头,带着老头帽,今天不开心,只收纸箱子,其他的都不要
// 第一个顾客出现了,他有这些破烂准备卖给我:
List<JunkDTO> junks = new ArrayList<>();
JunkDTO wpJunk = new JunkDTO("瓦片",10D);
JunkDTO jtJunk = new JunkDTO("金条",100.02D);
junks.add(wpJunk);
junks.add(jtJunk);
// 我去挑一挑
Filter f = new PaperFilter();
junks = f.f(junks);
if(junks == null || junks.size() <= 0){
System.out.println("啥也不是,穷酸");
}else{
System.out.println("发财啦发财啦");
}
}
}
package com.pattern.FilterPattern.materials.filter;
import com.pattern.FilterPattern.materials.po.JunkDTO;
import java.util.List;
/**
* 过滤接口
*
* @author lx
*/
public interface Filter {
/**
* 纸箱子破烂
*/
String PAPER = "paper";
/**
* 塑料制品破烂
*/
String PLASTICS = "plastics";
/**
* 过滤
*
* @param junks 破烂列表
* @return 想要的破烂列表
*/
List<JunkDTO> f(List<JunkDTO> junks);
}
package com.pattern.FilterPattern.materials.filter;
import com.pattern.FilterPattern.materials.po.JunkDTO;
import java.util.List;
import java.util.stream.Collectors;
/**
* 纸箱子过滤
*
* @author lx
* @date 2021/11/25 14:03
**/
public class PaperFilter implements Filter {
/**
* 过滤
*
* @param junks 破烂列表
* @return 想要的破烂列表
*/
@Override
public List<JunkDTO> f(List<JunkDTO> junks) {
return junks
.parallelStream()
.filter(f -> PAPER.equals(f.getName()))
.collect(Collectors.toList());
}
}
package com.pattern.FilterPattern.materials.po;
/**
* 破烂
*
* @author lx
* @date 2021/11/25 13:59
**/
public class JunkDTO {
/**
* 破烂名字
*/
private String name;
/**
* 破烂重量 斤
*/
private Double weight;
public JunkDTO(String name, Double weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "JunkDTO{" +
"name='" + name + '\'' +
", weight=" + weight +
'}';
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册