Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions design_pattern/src/main/java/example/strategy/BitcoinPayment.java

This file was deleted.

30 changes: 30 additions & 0 deletions design_pattern/src/main/java/example/strategy/Clothing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package example.strategy;

public class Clothing {
private String name;
private String type; // top, bottom, outer 등
private int price;

public Clothing(String name, String type, int price) {
this.name = name;
this.type = type;
this.price = price;
}

public String getName() {
return name;
}

public String getType() {
return type;
}

public int getPrice() {
return price;
}

@Override
public String toString() {
return name + " [" + type + "] - " + price + "원";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package example.strategy;

import java.util.List;

public class ClothingFilterContext {
private ClothingFilterStrategy strategy;

public void setStrategy(ClothingFilterStrategy strategy) {
this.strategy = strategy;
}

public List<Clothing> filterClothing(List<Clothing> clothes) {
if (strategy == null) {
throw new IllegalStateException("전략이 설정되지 않았습니다.");
}
return strategy.filter(clothes);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package example.strategy;

import java.util.List;

public interface ClothingFilterStrategy {
List<Clothing> filter(List<Clothing> clothes);
}


This file was deleted.

14 changes: 0 additions & 14 deletions design_pattern/src/main/java/example/strategy/PayPalPayment.java

This file was deleted.

17 changes: 0 additions & 17 deletions design_pattern/src/main/java/example/strategy/PaymentContext.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package example.strategy;

import java.util.List;
import java.util.stream.Collectors;

public class PriceBelowFilter implements ClothingFilterStrategy {
private int maxPrice;

public PriceBelowFilter(int maxPrice) {
this.maxPrice = maxPrice;
}

@Override
public List<Clothing> filter(List<Clothing> clothes) {
return clothes.stream()
.filter(c -> c.getPrice() <= maxPrice)
.collect(Collectors.toList());
}
}

13 changes: 13 additions & 0 deletions design_pattern/src/main/java/example/strategy/TopFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package example.strategy;

import java.util.List;
import java.util.stream.Collectors;

public class TopFilter implements ClothingFilterStrategy {
@Override
public List<Clothing> filter(List<Clothing> clothes) {
return clothes.stream()
.filter(c -> "top".equalsIgnoreCase(c.getType()))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package example.strategy;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

class ClothingStrategyTest {

@Test
void clothingStrategyTest() {
List<Clothing> clothes = Arrays.asList(
new Clothing("반팔 티셔츠", "top", 25000),
new Clothing("청바지", "bottom", 55000),
new Clothing("가디건", "outer", 75000),
new Clothing("셔츠", "top", 49000),
new Clothing("코트", "outer", 150000)
);

ClothingFilterContext context = new ClothingFilterContext();

// 상의만 필터링
context.setStrategy(new TopFilter());
System.out.println("=== 상의만 필터링 ===");
context.filterClothing(clothes).forEach(System.out::println);

// 5만원 이하 필터링
context.setStrategy(new PriceBelowFilter(50000));
System.out.println("\n=== 5만 원 이하만 필터링 ===");
context.filterClothing(clothes).forEach(System.out::println);
}
}

This file was deleted.