예제 #1
0
    public static void CriteriaPatternDemo()
    {
        Person p1 = new Person("Aykut", 24, Gender.MALE);
        Person p2 = new Person("Yilmaz", 55, Gender.MALE);
        Person p3 = new Person("Esra", 50, Gender.FEMALE);
        Person p4 = new Person("Zeynep", 12, Gender.FEMALE);

        List <Person> family = new List <Person>()
        {
            p1, p2, p3, p4
        };

        ICriteria <Person> maleCriteria   = new MaleCriteria();
        ICriteria <Person> femaleCriteria = new FemaleCriteria();
        ICriteria <Person> teenCriteria   = new TeenCriteria();

        var males   = maleCriteria.Filter(family).ToList();
        var females = femaleCriteria.Filter(family).ToList();
        var teens   = teenCriteria.Filter(family).ToList();

        ICriteria <Person> maleAndTeenCriteria = new AndCriteria(maleCriteria, teenCriteria);
        var teenMales = maleAndTeenCriteria.Filter(family);

        ICriteria <Person> femaleAndTeenCriteria = new AndCriteria(femaleCriteria, teenCriteria);
        var teenFemales = femaleAndTeenCriteria.Filter(family);

        ICriteria <Person> maleOrTeenCriteria = new OrCriteria(maleCriteria, teenCriteria);
        var malesOrTeens = maleOrTeenCriteria.Filter(family);
    }