过滤器模式

过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。

我们将创建一个 SpicySlices对象、Criteria 接口和实现了该接口的实体类,来过滤 SpicySlices对象的列表。NameCriteria类中使用 Criteria 对象,基于各种标准和它们的结合来过滤 SpicySlices对象的列表。

辣片对象类

package 过滤器模式;
/**
 * 辣片
 * @author wangqiangac
 * @verion NCC-1.0
 * @since 2021-05-19 10:27:18
 */
public class SpicySlices {
	private String name;
	private Double price;
	private Double weight;
	
	public SpicySlices(String name,Double price,Double weight){
		this.name = name;
		this.price = price;
		this.weight = weight;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public Double getWeight() {
		return weight;
	}
	public void setWeight(Double weight) {
		this.weight = weight;
	}
	
}

过滤器接口类

package 过滤器模式;

import java.util.List;

/**
 * 为标准(Criteria)创建一个接口。
 * @author wangqiangac
 * @verion NCC-1.0
 * @since 2021-05-19 10:33:20
 */
public interface Criteria {
	public List<SpicySlices> filterCriteria(List<SpicySlices> persons);
}

价格过滤类

package 过滤器模式;

import java.util.ArrayList;
import java.util.List;
/**
 * 辣片价格过滤器
 * @author wangqiangac
 * @verion NCC-1.0
 * @since 2021-05-19 10:35:59
 */
public class PriceCriteria implements Criteria {
	private Double price;
	public PriceCriteria(Double price) {
		this.price = price;
	}
	@Override
	public List<SpicySlices> filterCriteria(List<SpicySlices> spicySlicess) {
		List<SpicySlices> pricePicySlices = new ArrayList<SpicySlices>();
		for (SpicySlices spicySlices : spicySlicess) {
			if(spicySlices.getPrice()>this.price) {
				pricePicySlices.add(spicySlices);
			}
		}
		return pricePicySlices;
	}
}

名称过滤类---其中main方法就是数据组装,以及过滤器的使用

package 过滤器模式;

import java.util.ArrayList;
import java.util.List;
/**
 * 辣片名称过滤器
 * @author wangqiangac
 * @verion NCC-1.0
 * @since 2021-05-19 10:35:59
 */
public class NameCriteria implements Criteria {
	private String name;
	public NameCriteria(String name){
		this.name = name;
	}
	@Override
	public List<SpicySlices> filterCriteria(List<SpicySlices> spicySlicess) {
		List<SpicySlices> weiLongSpicySlices = new ArrayList<SpicySlices>();
		for (SpicySlices spicySlices : spicySlicess) {
			if(this.name.equalsIgnoreCase(spicySlices.getName())) {
				weiLongSpicySlices.add(spicySlices);
			}
		}
		return weiLongSpicySlices;
	}
	
	
	public static void main(String[] args) {
		List<SpicySlices> spicySlicess = new ArrayList<SpicySlices>();
		spicySlicess.add(new SpicySlices("卫龙",new Double(10),new Double(10)));
		spicySlicess.add(new SpicySlices("卫龙",new Double(20),new Double(20)));
		spicySlicess.add(new SpicySlices("乡巴佬",new Double(10),new Double(10)));
		spicySlicess.add(new SpicySlices("乡巴佬",new Double(20),new Double(55)));
		NameCriteria nameCriteria1= new NameCriteria("卫龙");
		System.out.println("过滤出卫龙");
		printspicySlicess(nameCriteria1.filterCriteria(spicySlicess));
		NameCriteria nameCriteria2= new NameCriteria("乡巴佬");
		System.out.println("过滤出乡巴佬");
		printspicySlicess(nameCriteria2.filterCriteria(spicySlicess));
		
		System.out.println("过滤出卫龙价格大于10的");
		PriceCriteria priceCriteria = new PriceCriteria(new Double(10));
		printspicySlicess(priceCriteria.filterCriteria(nameCriteria1.filterCriteria(spicySlicess)));
	}
	
	public static void printspicySlicess(List<SpicySlices> spicySlicess) {
		System.out.println("SpicySlices:[");
		for (SpicySlices spicySlices : spicySlicess) {
			System.out.println("name:"+spicySlices.getName()+",price:"+spicySlices.getPrice()+",weight:"+spicySlices.getWeight());
		}
		System.out.println("]");
	}
}

执行结果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值