Skip to content

Instantly share code, notes, and snippets.

@hongbeomi
Created November 26, 2019 07:59
Show Gist options
  • Save hongbeomi/a829c598fa430642fa87b7a327f59c8b to your computer and use it in GitHub Desktop.
Save hongbeomi/a829c598fa430642fa87b7a327f59c8b to your computer and use it in GitHub Desktop.
// 프레디케이트 객체로 사과 검사 조건을 캡슐화하기
public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p) {
List<Apple> result = new ArrayList<>();
for(Apple apple: inventory) {
if(p.test(apple)) {
result.add(apple);
}
}
return result;
}
// Apple 속성과 관련한 모든 변화에 대응할 수 있는 유연한 코드
public class AppleRedAndHeavyPredicate implements ApplePredicate {
public boolean test(Apple apple){
return RED.equals(apple.getColor()) && apple.getWeight() > 150;
}
}
// 전달한 ApplePredicate 객체에 의해 fileterApples 메서드의 동작이 결정된다!
List<Apple> redAndHeavyApples = filterApples(inventory, new AppleRedAndHeavyPredicate());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment