23种设计模式之过滤模式

/**
 * 1.模式定义:
 *   过滤器(Filter Pattern)又称为标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,
 *   通过预算逻辑以解耦的方式将他们联系起来。这种类型的设计模式属于结构模型,说白了,就是按条件筛选一组对象出来。
 *   目的:使用不同标准来过滤一组对象
 *   实现:制定不同的规则来实现过滤,然后对过滤结果进行分组。
 * 2.组成角色:
 *   1)抽象过滤器角色(AbstractFilter):负责定义过滤器的实现接口,具体的实现还要具体过滤器角色去参与,客户端可以调用抽象过滤器角色中定义好
 *     的方法,将 客户端的所有请求委派到具体的实现类去,从而让实现类去处理。
 *   2)ConcreteFilter(具体过滤角色):该角色负责具体筛选规则的逻辑实现,最后再返回一个过滤后的数据集合,标准的过滤器只对数据做过滤,当然也
 *     可以对集合中的数据做某项处理,再将处理后的集合返回。
 *   3)Subject(被过滤的主体角色):一个软件系统中可以有一个或多个目标角色,在具体过滤器角色中对指定的目标进行处理。
 * 3.过滤器延伸:
 *   过滤器链:携带多个过滤器,并且可以以自定义顺序执行他们。
 *   过滤器管理角色:负责管理过滤器和过滤器链。
 * 4.应用场景:
 *   垃圾桶分类
 * 5.总结:
 *   1)可插拔:过滤器的设计概念要求其实支持可插拔设计的。
 *   2)有序性:过滤器是被设计为一组组的过滤装置,要实现数据过滤,就必须有序性要求.
 *   3)过滤器的独立性:每种过滤器必须是独立的实体,其状态不受其它过滤器的影响,每个过滤器都有自己独立的数据输入输出接口,只要各个过滤器之间
 *     传送的数据遵守共同的规约就可以相连接。
 */

/*
 * 垃圾類,被過濾的主題角色
 */
public class Rubbish {
    private String name; //垃圾名稱
    private boolean isHarm; //是否有害垃圾
    private boolean isRecycled; //是否可回收
    private boolean isDry; //是否干垃圾
    private boolean isWet; //是否湿垃圾
    
    public Rubbish(String name, boolean isHarm, boolean isRecycled, boolean isDry, boolean isWet) {
        this.name = name;
        this.isHarm = isHarm;
        this.isRecycled = isRecycled;
        this.isDry = isDry;
        this.isWet = isWet;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public boolean isHarm() {
        return isHarm;
    }
    public void setHarm(boolean isHarm) {
        this.isHarm = isHarm;
    }
    public boolean isRecycled() {
        return isRecycled;
    }
    public void setRecycled(boolean isRecycled) {
        this.isRecycled = isRecycled;
    }
    public boolean isDry() {
        return isDry;
    }
    public void setDry(boolean isDry) {
        this.isDry = isDry;
    }
    public boolean isWet() {
        return isWet;
    }
    public void setWet(boolean isWet) {
        this.isWet = isWet;
    }
}

/*
 * 过滤标准的接口,即抽象过滤角色
 */
public interface Criteria {
    //定义过滤的标准
    List<Rubbish> rubbishFilter(List<Rubbish> rubbishs);

}

/*
 * 实现Criteria接口的实体类
 */
//干垃圾
public class DryRubbishCriteria implements Criteria {

    @Override
    public List<Rubbish> rubbishFilter(List<Rubbish> rubbishs) {
        List<Rubbish> rubbishList=new ArrayList<Rubbish>();
        for(Rubbish rubbish:rubbishs) {
            //这里只过滤出所有干垃圾
            if (rubbish.isDry()) {
                rubbishList.add(rubbish);
            }
        }
        return rubbishList;
    }

}

//有害垃圾
public class HarmfulRubbishCriteria implements Criteria {

    @Override
    public List<Rubbish> rubbishFilter(List<Rubbish> rubbishs) {
        List<Rubbish> rubbishList=new ArrayList<Rubbish>();
        for(Rubbish rubbish:rubbishs) {
            //这里只过滤出所有干垃圾
            if (rubbish.isHarm()) {
                rubbishList.add(rubbish);
            }
        }
        return rubbishList;
    }

}

//可回收垃圾
public class RecycledRubbishCriteria implements Criteria{

    @Override
    public List<Rubbish> rubbishFilter(List<Rubbish> rubbishs) {
        List<Rubbish> rubbishList=new ArrayList<Rubbish>();
        for(Rubbish rubbish:rubbishs) {
            //这里只过滤出所有干垃圾
            if (rubbish.isRecycled()) {
                rubbishList.add(rubbish);
            }
        }
        return rubbishList;
    }

}

//湿垃圾
public class WetRubbishCriteria implements Criteria{

    @Override
    public List<Rubbish> rubbishFilter(List<Rubbish> rubbishs) {
        List<Rubbish> rubbishList=new ArrayList<Rubbish>();
        for(Rubbish rubbish:rubbishs) {
            //这里只过滤出所有干垃圾
            if (rubbish.isWet()) {
                rubbishList.add(rubbish);
            }
        }
        return rubbishList;
    }

}

测试结果如下:

//使用不同的标准(Criteria)来过滤Rubbish对象的列表
public class Main {
    public static void main(String[] args) {
        //原始数据集合
        List<Rubbish> rubbishList = new ArrayList<Rubbish>();
        rubbishList.add(new Rubbish("果壳", false, false, true, false));
        rubbishList.add(new Rubbish("陶瓷", false, false, true, false));
        rubbishList.add(new Rubbish("菜根菜叶", false, false, false, true));
        rubbishList.add(new Rubbish("果皮", false, false, false, true));
        rubbishList.add(new Rubbish("电池", true, false, false, false));
        rubbishList.add(new Rubbish("水银温度计", true, false, false, false));
        rubbishList.add(new Rubbish("灯泡", true, false, false, false));
        rubbishList.add(new Rubbish("废纸塑料", false, true,false, false));
        rubbishList.add(new Rubbish("金属和布料", false, true, false, false));
        //四种不同的过滤标准
        Criteria dryRubbishCriteria=new DryRubbishCriteria();
        Criteria wetRubbishCriteria=new WetRubbishCriteria();
        Criteria harRubbishCriteria=new HarmfulRubbishCriteria();
        Criteria recycledRubbishCriteria=new RecycledRubbishCriteria();
        
        System.out.println("干垃圾:");
        printRubbishes(dryRubbishCriteria.rubbishFilter(rubbishList));
        System.out.println("湿垃圾:");
        printRubbishes(wetRubbishCriteria.rubbishFilter(rubbishList));
        System.out.println("有害垃圾:");
        printRubbishes(harRubbishCriteria.rubbishFilter(rubbishList));
        System.out.println("可回收垃圾:");
        printRubbishes(recycledRubbishCriteria.rubbishFilter(rubbishList));
        
    }
    private static void printRubbishes(List<Rubbish> rubbishs) {
        for(Rubbish rubbish:rubbishs) {
            System.out.println(rubbish.getName());
        }
        
    }

}

干垃圾:
果壳
陶瓷
湿垃圾:
菜根菜叶
果皮
有害垃圾:
电池
水银温度计
灯泡
可回收垃圾:
废纸塑料
金属和布

相关推荐