// 기존의 메서드 형태 publicbooleanonlyParamater(int appleWeight, int compareWeight){ return appleWeight > compareWeight ? true : false; }
/////// Apple 정보를 담는 클래스 classApple{ privateint weight; private String color; publicApple(int weight, String color){ this.weight = weight; this.color = color; } ///Getter, Setter }
/////// Apple 정보에 대해 참거짓을 반환하는 프레디케이트 인터페이스 interfaceApplePredicate{ booleantest(Apple apple); }
/////// 프레디케이트를 구현한 클래스 classAppleHeavyWeightPredicateimplementsApplePredicate{ publicbooleantest(Apple apple){ return"green".equals(apple.getColor()); } }
/////// 동작 전달을 위한 인터페이스 파라미터를 받는 메서드 classAppleUtil{ publicstatic 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; } }
//////// 동작 파라메터 publicclassShowAppleItem{ publicstaticvoidmain(String []args){ List<Apple> inventory = Arrays.asList(new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red")); List<Apple> result = AppleUtil.filterApples(inventory, new AppleHeavyWeightPredicate()); } }
위 코드를 보면동작의 핵심 이외의 장황한 코드가 필연적이므로 구현하고 유지보수하는 데 어려움이 많은 문제점이 있다.