학습내용

http://blog.chequer.io/archives/105

의 내용을 토대로 예제를 만들었습니다.

A-01. 사과엔티티

 package com.donzbox;


public class Apple {

    

    private String color;

    private int weight;


    public String getColor() {

        return color;

    }


    public void setColor(String color) {

        this.color = color;

    }


    public int getWeight() {

        return weight;

    }


    public void setWeight(int weight) {

        this.weight = weight;

    }

}

A-02. 오렌지엔티티

 package com.donzbox;


public class Orange {

    private String color;

    private int weight;


    public String getColor() {

        return color;

    }


    public void setColor(String color) {

        this.color = color;

    }


    public int getWeight() {

        return weight;

    }


    public void setWeight(int weight) {

        this.weight = weight;

    }

}

A-03. 사과와 오렌지 엔티티의 데이터를 필터조건에 의해 추출

 package com.donzbox;


import java.util.ArrayList;

import java.util.List;

import java.util.ListIterator;


public class FruitsFactory {


    public static void main(String[] args) {

        FruitsFactory appleFactory = new FruitsFactory();

        appleFactory.TestApp1();

    }


    public List<Apple> AppleData() {

        List<Apple> appleList = new ArrayList<>();

        Apple apple = new Apple();

        apple.setColor("red");

        apple.setWeight(40);

        appleList.add(apple);

        apple = new Apple();

        apple.setColor("green");

        apple.setWeight(60);

        appleList.add(apple);

        return appleList;

    }


    public List<Orange> OrangeData() {

        List<Orange> orangeList = new ArrayList<>();

        Orange orange = new Orange();

        orange.setColor("yellow");

        orange.setWeight(40);

        orangeList.add(orange);

        orange = new Orange();

        orange.setColor("blue");

        orange.setWeight(60);

        orangeList.add(orange);

        return orangeList;

    }


    public List<Apple> filterGreenApples(List<Apple> inventory, String color, int weight, boolean flag) {

        List result = new ArrayList<>();

        for (Apple apple : inventory) {

            if ((flag && apple.getColor().equals(color)) || (!flag && apple.getWeight() > weight)) {

                result.add(apple);

            }

        }

        return result;

    }


    public void TestApp1() {

        List<Apple> result = filterGreenApples(AppleData(), "red", 50, true);

        ListIterator<Apple> litr = result.listIterator();

        while (litr.hasNext()) {

            Apple apple = litr.next();

            System.out.println(apple.getColor() + " / " + apple.getWeight());

        }

    }

}


B-01. 사과 추상화

 package com.donzbox;


import java.util.ArrayList;

import java.util.List;

import java.util.ListIterator;


public class FruitsFactory {


    public static void main(String[] args) {

        FruitsFactory appleFactory = new FruitsFactory();

        appleFactory.TestApp2();

    }


    public List<Apple> AppleData() {

        List<Apple> appleList = new ArrayList<>();

        Apple apple = new Apple();

        apple.setColor("red");

        apple.setWeight(40);

        appleList.add(apple);

        apple = new Apple();

        apple.setColor("green");

        apple.setWeight(60);

        appleList.add(apple);

        return appleList;

    }


    public List<Orange> OrangeData() {

        List<Orange> orangeList = new ArrayList<>();

        Orange orange = new Orange();

        orange.setColor("yellow");

        orange.setWeight(40);

        orangeList.add(orange);

        orange = new Orange();

        orange.setColor("blue");

        orange.setWeight(60);

        orangeList.add(orange);

        return orangeList;

    }



    public interface ApplePredicate {

        public boolean test(Apple a);

    }


    public class AppleWeightPredicate implements ApplePredicate {

        public boolean test(Apple apple) {

            return apple.getWeight() > 150;

        }

    }


    public class AppleColorPredicate implements ApplePredicate {

        public boolean test(Apple apple) {

            return "green".equals(apple.getColor());

        }

    }


    public class AppleRedAndHeavyPredicate implements ApplePredicate {

        public boolean test(Apple apple) {

            return "red".equals(apple.getColor()) && apple.getWeight() > 150;

        }

    }


    public List filterApples(List<Apple> inventory, ApplePredicate p) {

        List result = new ArrayList<>();

        for (Apple apple : inventory) {

            if (p.test(apple)) {

                result.add(apple);

            }

        }

        return result;

    }


    public void TestApp2() {

        List result = filterApples(AppleData(), new AppleColorPredicate());


        result = filterApples(AppleData(), (Apple apple) -> "red".equals(apple.getColor()));


        ListIterator<Apple> litr = result.listIterator();

        while (litr.hasNext()) {

            Apple apple = litr.next();

            System.out.println(apple.getColor() + " / " + apple.getWeight());

        }

    }

}



C-01. 사과 또는 오렌지 뭐든지간에 드오는대로 처리되는 추상화

과일 구현체에 의한 사과

 package com.donzbox;


public class Apple implements Flute {

    

    private String color;

    private int weight;


    public String getColor() {

        return color;

    }


    public void setColor(String color) {

        this.color = color;

    }


    public int getWeight() {

        return weight;

    }


    public void setWeight(int weight) {

        this.weight = weight;

    }

}

과일 구현체에 의한 오렌지

 package com.donzbox;


public class Orange implements Flute {

    private String color;

    private int weight;


    public String getColor() {

        return color;

    }


    public void setColor(String color) {

        this.color = color;

    }


    public int getWeight() {

        return weight;

    }


    public void setWeight(int weight) {

        this.weight = weight;

    }

}

과일 인터페이스

 package com.donzbox;


public interface Fruits {

public String getColor();

public int getWeight();

}



T클레스에 의한 필터처리

 package com.donzbox;


import java.util.ArrayList;

import java.util.List;

import java.util.ListIterator;


public class FruitsFactory {


    public static void main(String[] args) {

        FruitsFactory appleFactory = new FruitsFactory();

        appleFactory.TestApp3();

    }


    public List<Apple> AppleData() {

        List<Apple> appleList = new ArrayList<>();

        Apple apple = new Apple();

        apple.setColor("red");

        apple.setWeight(40);

        appleList.add(apple);

        apple = new Apple();

        apple.setColor("green");

        apple.setWeight(60);

        appleList.add(apple);

        return appleList;

    }


    public List<Orange> OrangeData() {

        List<Orange> orangeList = new ArrayList<>();

        Orange orange = new Orange();

        orange.setColor("yellow");

        orange.setWeight(40);

        orangeList.add(orange);

        orange = new Orange();

        orange.setColor("blue");

        orange.setWeight(60);

        orangeList.add(orange);

        return orangeList;

    }



    public interface Predicate<T> {

        boolean test(T t);

    }


    public class WeightPredicate<T extends Fruits> implements Predicate<T> {

        public boolean test(T t) {

            return t.getWeight() > 150;

        }

    }


    public class ColorPredicate<T extends Fruits> implements Predicate<T> {

        public boolean test(T t) {

            return "green".equals(t.getColor());

        }

    }


    public class RedAndHeavyPredicate<T extends Fruits> implements Predicate<T> {

        public boolean test(T t) {

            return "red".equals(t.getColor()) && t.getWeight() > 150;

        }

    }


    public <X> List<X> filter(List<X> list, Predicate<X> p) {

        List<X> result = new ArrayList<>();

        for (X e : list) {

            if (p.test(e)) {

                result.add(e);

            }

        }

        return result;

    }


    public void TestApp3() {

        List<Apple> resultApple = filter(AppleData(), new ColorPredicate<>());

        List<Orange> resultOrange = filter(OrangeData(), new ColorPredicate<>());


        ListIterator<Apple> litrApple = resultApple.listIterator();

        while (litrApple.hasNext()) {

            Apple apple = litrApple.next();

            System.out.println(apple.getColor() + " / " + apple.getWeight());

        }

        

        ListIterator<Orange> litrOrange = resultOrange.listIterator();

        while (litrOrange.hasNext()) {

            Orange orange = litrOrange.next();

            System.out.println(orange.getColor() + " / " + orange.getWeight());

        }


//        List redApples = filter(AppleData(), (Apple apple) -> "red".equals(apple.getColor()));

//        List heavyOranges = filter(OrangeData(), (Orange orange) -> orange.getWeight() > 500);

    }

}




+ Recent posts