设计模式-过滤器模式

过滤器模式



过滤器模式属于结构型模式

它使用一个或多个标准,即过滤器来过滤一组对象,

来得到满足标准的对象。

场景

在一组汽车中,我们要过滤出品牌是吉利,并且颜色是红色的汽车。

实例

汽车Car

package com.superv.resource.design.filter;

/**
 * @author yangwei
 * @description 汽车 
 *
 * @date 2020年12月26日-下午4:54:27
 */
public class Car {

	/**
	 * 品牌
	 */
	private String brand;
	
	/**
	 * 颜色
	 */
	private String color;

	public Car(String brand, String color) {
		super();
		this.brand = brand;
		this.color = color;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	@Override
	public String toString() {
		return "Car [brand=" + brand + ", color=" + color + "]";
	}
	
}

过滤器接口CarFilter

package com.superv.resource.design.filter;

import java.util.List;

/**
 * @author yangwei
 * @description 过滤器接口
 *
 * @date 2020年12月26日-下午4:55:44
 */
public interface CarFilter {

	/**
	 * 过滤符合条件的汽车
	 * @param cars
	 * @return
	 */
	public List<Car> filter(List<Car> cars);
	
}

吉利过滤器JiliFilter

package com.superv.resource.design.filter;

import java.util.Iterator;
import java.util.List;

/**
 * @author yangwei
 * @description 吉利过滤器(过滤出品牌是吉利的汽车)
 *
 * @date 2020年12月26日-下午4:56:19
 */
public class JiliFilter implements CarFilter {

	@Override
	public List<Car> filter(List<Car> cars) {
		// TODO Auto-generated method stub
		for (Iterator<Car> iterator = cars.iterator(); iterator.hasNext();) {
			Car car = (Car) iterator.next();
			if (!"jili".equals(car.getBrand())) {
				iterator.remove();
			}
		}
		return cars;
	}

}

红色过滤器

package com.superv.resource.design.filter;

import java.util.Iterator;
import java.util.List;

/**
 * @author yangwei
 * @description 红色过滤器(过滤出颜色是红色的汽车)
 *
 * @date 2020年12月26日-下午4:58:44
 */
public class RedFilter implements CarFilter {

	@Override
	public List<Car> filter(List<Car> cars) {
		// TODO Auto-generated method stub
		for (Iterator<Car> iterator = cars.iterator(); iterator.hasNext();) {
			Car car = (Car) iterator.next();
			if (!"red".equals(car.getColor())) {
				iterator.remove();
			}
		}
		return cars;
	}

}

处理"且"关系的过滤器AndFilter

package com.superv.resource.design.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangwei
 * @description 处理且关系的过滤器
 *
 * @date 2020年12月26日-下午5:01:16
 */
public class AndFilter implements CarFilter {

	private CarFilter filter;
	
	private CarFilter anotherFilter;
	
	public AndFilter(CarFilter filter, CarFilter anotherFilter) {
		super();
		this.filter = filter;
		this.anotherFilter = anotherFilter;
	}

	@Override
	public List<Car> filter(List<Car> cars) {
		// TODO Auto-generated method stub
		List<Car> carList = filter.filter(cars);
		return anotherFilter.filter(carList);
	}

	/**
	 * 以下为测试部分
	 * @param args
	 */
	public static void main(String[] args) {
		List<Car> cars = new ArrayList<Car>();
		Car c1 = new Car("jili", "red");
		cars.add(c1);
		Car c2 = new Car("jili", "black");
		cars.add(c2);
		Car c3 = new Car("长安", "red");
		cars.add(c3);
		AndFilter andFilter = new AndFilter(new JiliFilter(), new RedFilter());
		List<Car> filterCars = andFilter.filter(cars);
		for (Car car : filterCars) {
			System.out.println(car);
		}
	}
	
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值