I read a lot about design patterns but i still got it difficult identifiying 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 wich 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, wich 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;
}
}
Aucun commentaire:
Enregistrer un commentaire