[CHAPTER 2] - 동작 파라미터화 코드 전달하기 #9
Replies: 6 comments 2 replies
-
2.1 변화하는 요구사항에 대응 시도들(1~3차)
// 2.1.2 색을 파라미터화
public static List<Apple> filterApplesByColor(List<Apple> inventory, Color color) {
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory) {
if (apple.getColor() == color) {
result.add(apple);
}
}
return result;
}
// 추가 요구사항 - 무게
public static List<Apple> filterApplesByWeight(List<Apple> inventory, int weight) {
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory) {
if (apple.getWeight() > weight) {
result.add(apple);
}
}
return result;
} → |
Beta Was this translation helpful? Give feedback.
-
2.2 동작 파라미터화 interface ApplePredicate {
boolean test(Apple a);
}
static class AppleWeightPredicate implements ApplePredicate {
@Override
public boolean test(Apple apple) {
return apple.getWeight() > 150;
}
}
static class AppleColorPredicate implements ApplePredicate {
@Override
public boolean test(Apple apple) {
return apple.getColor() == Color.GREEN;
}
}
2.2.1 네번째시도 : 추상적 조건으로 필터링 public static List<Apple> filter(List<Apple> inventory, ApplePredicate p) {
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory) {
if (p.test(apple)) {
result.add(apple);
}
}
return result;
}
static class AppleRedAndHeavyPredicate implements ApplePredicate {
@Override
public boolean test(Apple apple) {
return apple.getColor() == Color.RED && apple.getWeight() > 150;
}
}
한 개의 파라미터, 다양한 동작
|
Beta Was this translation helpful? Give feedback.
-
퀴즈 2-1public class PrettyPrintApple {
public static void main(String[] args) {
List<Apple> inventory = Arrays.asList(
new Apple(80, FilteringApples.Color.GREEN),
new Apple(155, FilteringApples.Color.GREEN),
new Apple(120, FilteringApples.Color.RED));
prettyPrintApple(inventory, new AppleFancyFormatter());
prettyPrintApple(inventory, new AppleSimpleFormatter());
}
public static void prettyPrintApple(List<Apple> inventory, AppleFormatter f) {
for (Apple apple : inventory) {
String output = f.accept(apple);
System.out.println(output);
}
}
public interface AppleFormatter {
String accept(Apple a);
}
public static class AppleFancyFormatter implements AppleFormatter {
@Override
public String accept(Apple apple) {
String characteristic = apple.getWeight() > 150 ?
"heavy" : "light";
return "A " + characteristic + " " + apple.getColor() + " apple";
}
}
public static class AppleSimpleFormatter implements AppleFormatter {
@Override
public String accept(Apple apple) {
return "An apple of " + apple.getWeight() + " g";
}
}
}
|
Beta Was this translation helpful? Give feedback.
-
2.3 복잡한 과정 간소화
// 익명 클래스
List<Apple> redApples2 = filter(inventory, new ApplePredicate() {
@Override
public boolean test(Apple a) {
return a.getColor() == RED;
}
});
2.3.3 6번째 시도 : 람다 표현식 사용List<Apple> result = filter(inventory, (Apple apple) -> RED.equals(apple.getColor())); 2.3.4 7번째 시도 : 리스트 형식으로 추상화 (
|
Beta Was this translation helpful? Give feedback.
-
2.4 실전예제
2.4.1 Comparator 로 정렬하기public interface Comparator<T>{
int compare(T o1, T o2);
}
>>>
inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
2.4.2 Runnable 로 코드 블록 실행하기
Thread t = new Thread(() -> sout("hello world)); |
Beta Was this translation helpful? Give feedback.
-
2.5 마치며
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
동작 파라미터화를 통해
동작 파라미터화 란
코드블록
코드블록
을 나중에 프로그램에서 호출함 =실행이 나중
으로 미뤄지는 것코드블록
에 따라 메서드 동작이파타미터
화 됨Beta Was this translation helpful? Give feedback.
All reactions