#κ°λ°/μλ°/λͺ¨λμλ°μΈμ‘μ
λμ νλΌλ―Έν°νλ₯Ό μ΄μ©νλ©΄ μμ£Ό λ°λλ μꡬμ¬νμ ν¨κ³Όμ μΌλ‘ λμν μ μμ΅λλ€.
λμ νλΌλ―Έν°λ? μμ§μ μ΄λ»κ² μ€νν κ²μΈμ§ κ²°μ νμ§ μμ μ½λ λΈλ‘μ΄λ€.
μλ₯Ό λ€μ΄, λμ€μ μ€νλ λ©μλμ μΈμλ‘ μ½λ λΈλ‘μ μ λ¬ν μ μμ΅λλ€. κ²°κ³Όμ μΌλ‘ μ½λ λΈλ‘μ λ°λΌ λ©μλμ λμμ΄ νλΌλ―Έν°ν λ©λλ€. μ΄λ₯Ό μ΄ν΄νκΈ° μν΄ μ± μ μλ νλμ μμ λ₯Ό ν΅ν΄ μμλ³΄κ² μ΅λλ€. κΈ°μ‘΄μ λμ₯ μ¬κ³ λͺ©λ‘ μ΄ν리μΌμ΄μ μ 리μ€νΈμμ λ Ήμ μ¬κ³Όλ§ νν°λ§ νλ κΈ°λ₯μ μΆκ°νλ€κ³ κ°μ ν©μλ€.
enum Color { RED, GREEN }
public static List<Apple> filterGreenApples(List<Apple> inventory) {
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory) {
if (GREEN.equals(apple.getColor()) {
result.add(apple);
}
}
return result;
}
κ·Έλ°λ° κ°μκΈ° λλΆκ° μ¬λ¬ κ°μ§ λ€μν μμΌλ‘ νν°λ§ νκ³ μΆμ΄μ§λ©΄, μ¬λ¬ λ©μλλ₯Ό λ§λ€κ³ ifλ¬Έμ μ¬λ¬κ° λ§λ€μ΄μΌ ν κΉμ? μ΄λ° μν©μμλ λ€μκ³Ό κ°μ μ’μ κ·μΉμ΄ μμ΅λλ€.
κ±°μ λΉμ·ν μ½λκ° λ°λ³΅ μ‘΄μ¬νλ€λ©΄ κ·Έ μ½λλ₯Ό μΆμννλ€.
2. μμ νλΌλ―Έν°ν
μμ νλΌλ―Έν°νν μ μλλ‘ λ©μλμ νλΌλ―Έν°λ₯Ό μΆκ°νλ©΄ λ³ννλ μꡬμ¬νμ μ’ λ μ μ°νκ² λμνλ μ½λλ₯Ό λ§λ€ μ μμ΅λλ€.
public static List<Apple> filterApplesByColor(List<Apple> inventory, Color color) {
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory) {
if (apple.getColor().equals(color)) {
result.add(apple);
}
}
return result;
}
κ·ΈλΌ λ€μμ²λΌ ꡬνν λ©μλλ₯Ό νΈμΆν μ μμ΅λλ€.
List<Apple> greenApples = filterApplesByColor(inventory, GREEN);
List<Apple> redApples = filterApplesByColor(inventory, RED);
κ·Έλ°λ° κ°μκΈ° λλΆκ° λ€μ λνλμλ 무κ²κ° 150κ·Έλ¨ μ΄μμΈ μ¬κ³Όκ° λ¬΄κ±°μ΄ μ¬κ³ΌμΈλ°, κ°λ²Όμ΄ μ¬κ³Όμ λ¬΄κ±°μ΄ μ¬κ³Όλ‘ ꡬλΆν μ μλ€λ©΄ μ’μ κ² κ°λ€κ³ ν©λλ€. μ΄μ μ, μμ ꡬννλ©΄μ λ€μν μμ λμν μ μλλ‘ λ©μλλ₯Ό λ§λ€μμΌλ νΉμ λͺ¨λ₯΄λ μꡬμ¬νμ λλΉνκΈ° μν΄ λ¬΄κ²λ μ¬λ¬ 무κ²λ₯Ό κΈ°μ€μΌλ‘ ꡬλΆνλλ‘ κ΅¬νν©λλ€.
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;
}
μ’μ ν΄κ²°μ± κ°μ΅λλ€. νμ§λ§, ꡬν μ½λλ₯Ό μμΈν보면 forλ¬ΈμΌλ‘ inventoryλ₯Ό νμνκ³ , κ° μ¬κ³Όμ νν°λ§ 쑰건μ μ μ©νλ λΆλΆμ μ½λκ° μ νν°λ§ μ½λμ λλΆλΆ μ€λ³΅λ©λλ€. κ·ΈλΌ, μ±λ₯ κ°μ μ μν΄ μ€λ³΅λλ λΆλΆμ μμ νλ €λ©΄ λͺ¨λ λ©μλλ₯Ό μμ νλ μ°Έμ¬κ° λ²μ΄μ§ κ² κ°λ€μ.
μμ μκ³Ό 무κ²λ₯Ό νλμ λ©μλλ‘ ν©μ³ filterλΌλ λ©μλλ₯Ό λ§λ€μ΄λ΄ μλ€.
3. κ°λ₯ν λͺ¨λ μμ±μΌλ‘ νν°λ§
public static List<Apple> filterApples(List<Apple> inventory, Color color,
int weight, boolean flag) {
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory) {
if ((flag && apple.getColor().equals(color)) ||
(!flag && apple.getWeight() > weight)) {
result.add(apple);
}
}
return result;
}
μ λ§ λλ¬μ΄ μ½λκ° λμ΄λ²λ Έμ΅λλ€. λλΆμ μꡬμ¬νμ΄ μ μ λμ΄λλ©΄ λ©μλμ μΈμλ μ μ λμ΄λκ² λ ν λ°, κ·Έλ¬λ©΄ ifλ¬Έμ λ μμ λμ΄μΌ ν©λλ€. μ΄λ₯Ό ν΄κ²°νκΈ° μν΄ λμ νλΌλ―Έν°νλ₯Ό μ΄μ©ν΄λ³΄λλ‘ ν©μλ€!
3.2 λμ νλΌλ―Έν°ν
μμ μμ λ₯Ό ν΅ν΄ νλΌλ―Έν° μΆκ°κ° μλ λ³ννλ μꡬμ¬νμ μ’ λ μ μ°νκ² λμν μ μλ λ°©λ²μ΄ νμνλ€λ κ²μ μκ²λμμ΅λλ€. μ ν 쑰건λ€μ μ’ λ λΆμν΄λ΄ μλ€.
μ¬κ³Όμ μμ±μ κΈ°μ΄ν΄ λΆλ¦¬μΈ κ°μ λ°νν μ μμ΅λλ€. (μ¬κ³Όκ° λ ΉμμΈκ°? yes-no, 무κ²κ° 150κ·Έλ¨ μ΄μμΈκ°? yes-no). μ΄λ κ² μ°Έ λλ κ±°μ§μ λ°ννλ ν¨μλ₯Ό νλ λμΌμ΄νΈλΌκ³ ν©λλ€. μ ν 쑰건μ κ²°μ νλ μΈν°νμ΄μ€λ₯Ό μ μν©μλ€.
public interface ApplePredicate {
boolean test (Apple apple);
}
κ·ΈλΌ λ€μμ²λΌ λ€μν μ ν쑰건μ λν΄ μ¬λ¬ λ²μ μ ApplePredicateλ₯Ό μ μν μ μμ΅λλ€.
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());
}
}
μ 쑰건μ λ°λΌ filter λ©μλκ° λ€λ₯΄κ² λμν κ²μ΄λΌκ³ μμν μ μμ΅λλ€. μ΄λ₯Ό μ λ΅ λμμΈ ν¨ν΄μ΄λΌκ³ ν©λλ€. μ΄κ²μ μ΄μ©ν΄μ filterApplesμμ ApplePredicate κ°μ²΄λ₯Ό λ°μ μ νμ 쑰건μ κ²μ¬νλλ‘ λ©μλλ₯Ό κ³ μ³λ΄ μλ€!
4. μΆμμ 쑰건μΌλ‘ νν°λ§
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;
}
μ²μμ μν©κ³Ό λΉκ΅νλ©΄ μμ μ μ°ν΄μ§ μ½λλ₯Ό λ³Ό μ μμ΅λλ€. μ°λ¦¬κ° μ λ¬ν ApplePredicate κ°μ²΄μ μν΄ filterApples λ©μλμ λμμ΄ κ²°μ λ©λλ€. μ¦, μ°λ¦¬λ filterApples λ©μλμ λμμ νλΌλ―Έν°ν ν κ²μ λλ€!
ν κ°μ νλΌλ―Έν°, λ€μν λμ
컬λ μ νμ λ‘μ§κ³Ό κ° νλͺ©μ μ μ©ν λμμ λΆλ¦¬νλ€λ κ²μ΄ 'λμ νλΌλ―Έν°'μ κ°μ μ λλ€.
'κ°λ°' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
MVC ν¨ν΄ (3) | 2024.10.15 |
---|---|
BeanFactoryμ ApplicationContext, Configuration (0) | 2024.10.04 |
μ€νλ§ λΆνΈλ₯Ό μ΄μ©ν νμ΄μ§ ꡬν (3) | 2024.09.27 |
JWT ν ν°μ μ΄μ©ν λ‘κ·ΈμΈ λ°©λ² μ΄ν΄ (5) | 2024.09.15 |