首页 > 其他分享 >8) Filter/Criteria Pattern

8) Filter/Criteria Pattern

时间:2023-06-06 13:34:54浏览次数:72  
标签:name Pattern List sex Filter Person marriage Criteria

类别:

 Structual Pattern

问题:

 

方案:

 

 

 

示例:

 

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CriteriaPatternDemo {
    public static void main(String[] args) {

        List<Person> persons = new ArrayList<Person>();
        persons.add(new Person("小强", "男", "未婚"));
        persons.add(new Person("小明", "男", "未婚"));
        persons.add(new Person("小李", "男", "已婚"));
        persons.add(new Person("小红", "女", "已婚"));
        persons.add(new Person("小月", "女", "已婚"));
        persons.add(new Person("小兰", "女", "未婚"));

        Criteria male = new Male();
        Criteria female = new Female();
        Criteria single = new Single();
        Criteria singleMale = new And(single, male);
        Criteria singleOrFemale = new Or(single, female);

        System.out.println("男: ");
        printPersons(male.filter(persons));
        System.out.println("\n女: ");
        printPersons(female.filter(persons));
        System.out.println("\n未婚男: ");
        printPersons(singleMale.filter(persons));
        System.out.println("\n未婚或者女: ");
        printPersons(singleOrFemale.filter(persons));
    }

    public static void printPersons(List<Person> persons) {
        for (Person person : persons) {
            System.out.println(person);
        }
    }
}

class Person {
    private String name;
    private String sex;
    private String marriage;

    public Person(String name, String sex, String marriage) {
        this.name = name;
        this.sex = sex;
        this.marriage = marriage;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public String getMarriage() {
        return marriage;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", sex=" + sex + ", marriage=" + marriage + "]";
    }

}

interface Criteria {
    public List<Person> filter(List<Person> list);
}

class Male implements Criteria {
    @Override
    public List<Person> filter(List<Person> list) {
        List<Person> malePersonList = new ArrayList<Person>();
        for (Person person : list) {
            if (person.getSex().equalsIgnoreCase("男")) {
                malePersonList.add(person);
            }
        }
        return malePersonList;
    }
}

class Female implements Criteria {
    @Override
    public List<Person> filter(List<Person> list) {
        List<Person> femalePersonList = new ArrayList<Person>();
        for (Person person : list) {
            if (person.getSex().equalsIgnoreCase("女")) {
                femalePersonList.add(person);
            }
        }
        return femalePersonList;
    }
}

class Single implements Criteria {
    @Override
    public List<Person> filter(List<Person> list) {
        List<Person> singlePersonList = new ArrayList<Person>();
        for (Person person : list) {
            if (person.getMarriage().equalsIgnoreCase("未婚")) {
                singlePersonList.add(person);
            }
        }
        return singlePersonList;
    }
}

class And implements Criteria {
    private Criteria a;
    private Criteria b;

    public And(Criteria a, Criteria b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public List<Person> filter(List<Person> list) {
        List<Person> aList = a.filter(list);
        return b.filter(aList);
    }
}

class Or implements Criteria {
    private Criteria a;
    private Criteria b;

    public Or(Criteria a, Criteria b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public List<Person> filter(List<Person> list) {
        List<Person> aList = a.filter(list);
        List<Person> bList = b.filter(list);
        Set<Person> set = new HashSet<Person>();
        set.addAll(aList);
        set.addAll(bList);
        return new ArrayList<Person>(set);
    }
}

 

男: 
Person [name=小强, sex=男, marriage=未婚]
Person [name=小明, sex=男, marriage=未婚]
Person [name=小李, sex=男, marriage=已婚]

女: 
Person [name=小红, sex=女, marriage=已婚]
Person [name=小月, sex=女, marriage=已婚]
Person [name=小兰, sex=女, marriage=未婚]

未婚男: 
Person [name=小强, sex=男, marriage=未婚]
Person [name=小明, sex=男, marriage=未婚]

未婚或者女: 
Person [name=小兰, sex=女, marriage=未婚]
Person [name=小月, sex=女, marriage=已婚]
Person [name=小强, sex=男, marriage=未婚]
Person [name=小明, sex=男, marriage=未婚]
Person [name=小红, sex=女, marriage=已婚]

 

应用:

 

不足:(

 

优化:)

 

标签:name,Pattern,List,sex,Filter,Person,marriage,Criteria
From: https://www.cnblogs.com/zno2/p/6694826.html

相关文章

  • 7) Bridge Pattern
    类别: StructuralPatterns问题:连连看的问题 不使用桥接,要写81个类,使用桥接,只需要18个类方案: 示例: publicclassBridgePatternDemo{publicstaticvoidmain(String[]args){Somebodysb=newXiaohong(newBanana());sb.eat();......
  • 6) Adapter Pattern
    类别: StructuralPattern问题:什么是接口?按照标准提供服务,其他想要使用该接口的要按照该标准接入服务什么是依赖?持有目标对象,拥有使用权(AuseB)数据线?数据线按USB标准接入充电宝,通过lightning提供充电服务手机?通过lightning标准接入数据线  客户端因种种限制只能接入......
  • 10) Decorator Pattern
    类别: StructuralPattern问题: 在不改变接口的前提下增加额外的服务方案:   示例:publicclassDecoratorPatternDemo{publicstaticvoidmain(String[]args){Shapecircle=newCircle();ShaperedCircle=newRedShapeDecorator(newC......
  • bloom filter
      在javaEyes上找到一篇挺有用的文章,希望能对大家理解Bloomfilter有帮助  1Overview   Bloomfilter最早由 BurtonHowardBloom提出,是一种用于判断成员是否存在于某个集合中的数据结构。 Bloomfilter的判断基于概率论:  如果某个成员存在于集合中,那么Bloomfilte......
  • bloomFilter_demo
    参考博客:(14条消息)布隆(BloomFilter)过滤器入门_布隆过滤器入门_qq_39093474的博客-CSDN博客5分钟搞懂布隆过滤器,亿级数据过滤算法你值得拥有!-知乎(zhihu.com)BloomFilterTest.javapackagecom.hmb;importcom.google.common.hash.BloomFilter;importcom.google.co......
  • cpp: Bridge Pattern
     /*****************************************************************//***\fileGold.h*\brief桥接模式BridgePatternC++14*2023年6月3日涂聚文GeovinDuVisualStudio2022edit.*\authorgeovindu*\dateJune2023***********************......
  • AS_Path Filter的应用
    华为:在同一个过滤器编号下,可以定义多条过滤规则(permit或deny模式)。在匹配过程中,这些规则之间是“或”的关系,即只要路由信息通过其中一项规则,就认为通过由该过滤器编号标识的这组AS_Path过滤器。在下文中,将分不同的场景来对AS_Path过滤器的作用进行举例说明。 表1BGPAS_Path......
  • lucene底层数据结构——底层filter bitset原理,时间序列数据压缩将同一时间数据压缩为
    如何联合索引查询?所以给定查询过滤条件age=18的过程就是先从termindex找到18在termdictionary的大概位置,然后再从termdictionary里精确地找到18这个term,然后得到一个postinglist或者一个指向postinglist位置的指针。然后再查询gender=女的过程也是类似的。最后得出age=18......
  • 如何使用Spring管理Filter和Servlet
    在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象的创建。如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用WebApplicationContextUtils.getRequiredWebApplicationContext......
  • spring cloud gateway 自定义GatewayFilterFactory
    官网地址:https://docs.spring.io/spring-cloud-gateway/docs/2.2.9.RELEASE/reference/html/#writing-custom-route-predicate-factories参考地址:https://blog.csdn.net/myli92/article/details/127328893importcom.ruoyi.common.core.utils.StringUtils;importorg.springfr......