Java/[Java8] 모던인자바액션

동작 파라미터화 코드 전달

쵼쥬 2022. 2. 9. 15:56

인터페이스를 사용해서 한개의 파라미터로 다양한 동작 가능

interface ApplePredicate {
    boolean test(Apple apple);
}

enum Color {
    RED, GREEN
}

class Apple {
    int weight;
    Color color;

    public Apple(int weight, Color color) {
        this.weight = weight;
        this.color = color;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}

class AppleHeavyWeightPredicate implements ApplePredicate {
    @Override
    public boolean test(Apple apple) {
        return apple.getWeight() > 150;
    }
}

class AppleGreenColorPredicate implements ApplePredicate {
    @Override
    public boolean test(Apple apple) {
        return GREEN.equals(apple.getColor());
    }
}

public class Main {
    public static void main(String args[]) throws IOException {
        List<Apple> inventory = Arrays.asList(new Apple(80, GREEN),
                new Apple(155, GREEN),
                new Apple(120, RED));

        List<Apple> heavyApples = filterApples(inventory, new AppleHeavyWeightPredicate());
        List<Apple> greenApples = filterApples(inventory, new AppleGreenColorPredicate());
    }

    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;
    }
}

 

 

 - 복잡한 과정 간소화

1 . 익명 클래스

List<Apple> heavyApples = filterApples(inventory, new ApplePredicate() {
    @Override
    public boolean test(Apple apple) {
        return apple.getWeight() > 150;
    }
});

List<Apple> greenApples = filterApples(inventory, new ApplePredicate() {
    @Override
    public boolean test(Apple apple) {
        return GREEN.equals(apple.getColor());
    }
});

아직 반복되는 코드가 지저분하다.

 

2 .람다 표현식 사용

인터페이스가 함수형 인터페이스인 경우에만 사용할 수 있다.

함수형 인터페이스 : 1개의 추상메소드를 가진 인터페이스

List<Apple> heavyApples = filterApples(inventory, (Apple apple) -> {
    return apple.getWeight() > 150;
});

List<Apple> greenApples = filterApples(inventory, (Apple apple) -> {
    return GREEN.equals(apple.getColor());
    
});

 

3. 리스트 형식으로 추상화

interface Predicate<T> {
    boolean test(T t);
}
static <T> List<T> filter(List<T> list, Predicate<T> p) {
    List<T> result = new ArrayList<>();
    for (T e : list) {
        if (p.test(e)) {
            result.add(e);
        }
    }
    return result;
}
List<Apple> redApples = filter(inventory, (Apple apple) -> RED.equals(apple.getColor()));
List<Integer> evenNumbers = filter(numbers, (Integer i) -> i % 2 == 0);

 

예) Comparator로 정렬하기

// java.util.Comparator
public interface Comparator<T>{
    int compare(T o1, T o2);
}
inventory.sort(new Comparator<Apple>() {
    @Override
    public int compare(Apple o1, Apple o2) {
        return o1.getWeight() - o2.getWeight();
    }
});

inventory.sort((Apple o1, Apple o2) -> o1.getWeight() - o2.getWeight());