1

I read a lot about design patterns but I still have difficulty identifying when I have to use them. Today I was reading oracle documentation about lambdas and saw a class "evolution" and said "Hey, clearly there's some decoupling here". I think there's a well known pattern here but don't know exactly which is.

Another question that I also have about this is, if I'm not using SPRING where the folders structure about interfaces and implementations is very clear, which would be -according goog practices- the project structure where I must create the interfaces.

The example started with this code:

public static void printPersonsOlderThan(List<Person> roster, int age) {
    for (Person p : roster) {
        if (p.getAge() >= age) {
            p.printPerson();
        }
    }
}

Then continued with this:

public static void printPersonsWithinAgeRange(
    List<Person> roster, int low, int high) {
    for (Person p : roster) {
        if (low <= p.getAge() && p.getAge() < high) {
            p.printPerson();
        }
    }
}

And ended with this:

public static void printPersons(
    List<Person> roster, CheckPerson tester) {
    for (Person p : roster) {
        if (tester.test(p)) {
            p.printPerson();
        }
    }
}

Created this interface:

interface CheckPerson {
    boolean test(Person p);
}

And this was the implementation:

class CheckPersonEligibleForSelectiveService implements CheckPerson {
    public boolean test(Person p) {
        return p.gender == Person.Sex.MALE &&
            p.getAge() >= 18 &&
            p.getAge() <= 25;
    }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
sebasm
  • 156
  • 10

3 Answers3

4

Basically you have implemented a filter like a Java FileFilter, this is close to the visitor pattern:

Could someone in simple terms explain to me the visitor pattern's purpose with examples if possible

Community
  • 1
  • 1
stacker
  • 68,052
  • 28
  • 140
  • 210
1

I passed Design Pattern subject at University with highest grade, still I can't seem to identify any familiar pattern.

Most likely, no predefined patterns are used, but the CheckPerson has been abstracted for obvious reasons.

At University, we group classes in packages, and Interfaces are usually placed in the same package with implementing classes.

JovanToroman
  • 605
  • 6
  • 16
-1

In addition to Visitor, you can consider to use Strategy pattern.

public abstract class PrintStrategy {

    abstract protected List<Person> checkPerson(List<Person> list);

    public void printPerson(List<Person> roster){

        List<Person> filteredRoster = this.checkPerson(roster);
        for (Person person : filteredRoster) {
            person.print();
        }
    }
}

public class PrintOlderThanStartegy extends PrintStrategy {

    private final int ageWaterMark;

    public PrintOlderThanStartegy(final int ageWaterMark){
        this.ageWaterMark = ageWaterMark;
    }

    protected List<Person> checkPerson(List<Person> roster) {

        List<Person> filteredRoster = new ArrayList<Person>();
        for (Person person : roster) {
            if(person.getAge() > ageWaterMark){
                filteredRoster.add(person);
            }
        }
        return filteredRoster;
    }

}

public class Test {

    public static void main(String[] args) {
        List<Person> roster = new ArrayList<Person>();

        Person p1 = new Person();
        p1.setAge(50);
        Person p2 = new Person();
        p2.setAge(20);

        roster.add(p1);
        roster.add(p2);

        PrintStrategy printStrategy = new PrintOlderThanStartegy(30);
        printStrategy.printPerson(roster);
    }

}
dstar55
  • 938
  • 6
  • 11