Skip to content

Instantly share code, notes, and snippets.

@hongbeomi
Created November 26, 2019 08:00
Show Gist options
  • Save hongbeomi/49efbbd596236700d609930de1933bb6 to your computer and use it in GitHub Desktop.
Save hongbeomi/49efbbd596236700d609930de1933bb6 to your computer and use it in GitHub Desktop.
// 무거운 사과 선택
public class AppleHeavyWeightPredicate implements ApplePredicate {
public boolean test(Apple apple) {
return apple.getWeight() > 150;
}
}
// 녹색 사과 선택
public class AppleGreenColorPredicate implements ApplePredicate {
public boolean test(Apple apple) {
return GREEN.equals(apple.getColor());
}
}
public class FilteringApples {
public static void main(String...args) {
List<Apple> inventory = Arrays.asList(new Apple(80, GREEN), new Apple(155, GREEN), new Apple(120, RED));
List<Apple> heavyApples = filterApples(inventory, new AppleHeavyWeightPredicate());
// 155그램의 사과 한 개
List<Apple> greenApples = filterApples(inventory, new AppleGreenColorPredicate());
// 녹색 사과 두 개
}
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;
}
}
// 로직과 관련없는 코드가 많이 추가되었다...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment