Java过滤器模式

过滤器模式用于对一组对象实现过滤。

首先定义一个过滤器接口。

然后为过滤器接口提供不同的实现。

再通过装饰器模式,允许不同的过滤器实现各种组合,如逻辑与逻辑或等。

示例代码:

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

// 集合元素
class Person {

	private String name;
	private String gender;
	private String maritalStatus;

	public Person(String name, String gender, String maritalStatus) {
		this.name = name;
		this.gender = gender;
		this.maritalStatus = maritalStatus;
	}

	public String getName() {
		return name;
	}

	public String getGender() {
		return gender;
	}

	public String getMaritalStatus() {
		return maritalStatus;
	}
}

// 标准过滤器抽象类,采用泛型,对Collection的具体类型没有限制,对Collection中的元素类型也没有限制
abstract class MyFilter {
	public abstract Collection filter(Collection mySet);

	@SuppressWarnings("unchecked")
	protected Collection getCache(Collection mySet) {
		Collection result = null;
		try {
			// 反射
			result = mySet.getClass().getDeclaredConstructor().newInstance();
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		return result;
	}
}

// 实现过滤器接口
class MyFilterMale extends MyFilter {
	public Collection filter(Collection mySet) {
		Collection result = getCache(mySet);
		if (result != null) {
			for (Person person : mySet) {
				if (person.getGender().equalsIgnoreCase("MALE")) {
					result.add(person);
				}
			}
		}
		return result;
	}
}

// 实现过滤器接口
class MyFilterSingle extends MyFilter {
	public Collection filter(Collection mySet) {
		Collection result = getCache(mySet);
		if (result != null) {
			for (Person person : mySet) {
				if (person.getMaritalStatus().equalsIgnoreCase("SINGLE")) {
					result.add(person);
				}
			}
		}
		return result;
	}
}

// 实现逻辑与过滤,采用泛型实现,可以重用,同时是装饰模式
class MyFilterLogicAnd extends MyFilter {
	private MyFilter con1;
	private MyFilter con2;

	public MyFilterLogicAnd(MyFilter con1, MyFilter con2) {
		this.con1 = con1;
		this.con2 = con2;
	}

	public Collection filter(Collection mySet) {
		return con2.filter(con1.filter(mySet));
	}
}

//实现逻辑或过滤,采用泛型实现,可以重用,同时是装饰模式
class MyFilterLogicOr extends MyFilter {
	private MyFilter con1;
	private MyFilter con2;

	public MyFilterLogicOr(MyFilter con1, MyFilter con2) {
		this.con1 = con1;
		this.con2 = con2;
	}

	public Collection filter(Collection mySet) {
		Collection temp1 = getCache(mySet);
		Collection temp2 = getCache(mySet);
		Collection result = getCache(mySet);
		if (temp1 != null) {
			temp1 = con1.filter(mySet);
			temp2 = con2.filter(mySet);
			for (T item : temp1) {
				if (!(result.contains(item))) {
					result.add(item);
				}
			}
			for (T item : temp2) {
				if (!(result.contains(item))) {
					result.add(item);
				}
			}
		}
		return result;
	}
}

public class Filter {

	public static void main(String[] args) {
		List persons = new ArrayList();

		persons.add(new Person("Robert", "Male", "Single"));
		persons.add(new Person("John", "Male", "Married"));
		persons.add(new Person("Laura", "Female", "Married"));
		persons.add(new Person("Diana", "Female", "Single"));
		persons.add(new Person("Mike", "Male", "Single"));
		persons.add(new Person("Bobby", "Male", "Single"));

		List malePersons = (List) new MyFilterMale().filter(persons);
		System.out.println("==========All Male==========");
		for (Person p : malePersons) {
			System.out.println(p.getName());
		}

		List maleAndSinglePersons = (List) new MyFilterLogicAnd(new MyFilterMale(),
				new MyFilterSingle()).filter(persons);
		System.out.println("==========All Male And Single==========");
		for (Person p : maleAndSinglePersons) {
			System.out.println(p.getName());
		}

		List maleOrSinglePersons = (List) new MyFilterLogicOr(new MyFilterMale(),
				new MyFilterSingle()).filter(persons);
		System.out.println("==========All Male Or Single==========");
		for (Person p : maleOrSinglePersons) {
			System.out.println(p.getName());
		}

	}

}

本例演示了过滤器模式

为了提高代码的可重用性及扩展性,用到了装饰器模式

同样,为了提高代码的可重用性,用到了泛型及反射机制。

当然如果处理的数据量比较大,还应该考虑多线程实现。

你可能感兴趣的:(Java语言)