-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
practice(practice): 여러 전략객체를 적용하는 패턴 공부
- Loading branch information
Showing
8 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package goodComposition; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
|
||
final Pattern pattern = Pattern.compile("file_check_\\d{1,2}"); | ||
final Matcher matcher = pattern.matcher("file_check_12 dddd"); | ||
if (!matcher.find()) { | ||
System.out.println("패턴이 발견되지 않았습니다."); | ||
return; | ||
} | ||
final String group = matcher.group(); | ||
System.out.println(group); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package practice; | ||
|
||
import goodComposition.Call; | ||
import goodComposition.Money; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class CalculatorBasedOnCalls { | ||
|
||
private final Set<CalculatorStrategy> strategies = new HashSet<>(); | ||
|
||
public CalculatorBasedOnCalls(final CalculatorStrategy strategy) { | ||
this.strategies.add(strategy); | ||
} | ||
|
||
public CalculatorBasedOnCalls setNext(final CalculatorStrategy strategy) { | ||
this.strategies.add(strategy); | ||
|
||
return this; | ||
} | ||
|
||
public Money calculateFee(final Set<Call> calls, Money result) { | ||
|
||
for (final CalculatorStrategy strategy : strategies) { | ||
result = result.plus(strategy.calculateFee(calls, result)); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package practice; | ||
|
||
import goodComposition.Call; | ||
import goodComposition.Money; | ||
import java.util.Set; | ||
|
||
public interface CalculatorStrategy { | ||
|
||
Money calculateFee(Set<Call> calls, Money money); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package practice; | ||
|
||
import goodComposition.Call; | ||
import goodComposition.Money; | ||
import java.util.Set; | ||
|
||
public class FirstStrategy implements CalculatorStrategy{ | ||
@Override | ||
public Money calculateFee(final Set<Call> calls, final Money money) { | ||
throw new UnsupportedOperationException("FirstStrategy#calculateFee not implemented."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package practice; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
//1. 정책이 적용될 객체는 확장(부모)가능성이 있는 class면, 생성자 주입을 받지 않는다. | ||
final Plan plan = new Plan(); | ||
|
||
//2. void setter받기기능으로 전략객체가 아닌 전략주입 구상클래스를 받는다. | ||
// -> 단순히 전략메서드만 필요한게 아니라, 템플릿메소드에서 변형된 것으로 생각하여 | ||
// --> 전략객체들의 공통기능들 or 전략조건들을 [여러개 받는] or [여러 전략객체들을 모아서 여러번 적용]할 이외 같은 형태를 만든다. | ||
// ---> 인터페이스로만 구성하면, 모아서 한번에 적용해주는 기능이 없다. | ||
//plan.setCalculator(new CalculatorBasedOnCalls()); | ||
|
||
//3.전략주입 구상클래스는 전략객체들을 받되, 1개는 필수로 받도록 생성장체서 받고 | ||
// -> 그 이후로는 setNext()를 통해 다음 것을 받는다. | ||
plan.setCalculator(new CalculatorBasedOnCalls(new FirstStrategy()) | ||
.setNext(new SecondStrategy()) | ||
.setNext(new ThirdStrategy()) | ||
); | ||
|
||
//4. 전략관련 주입받은 적용대상 plan이 계산을 한다. | ||
plan.calculateFee(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package practice; | ||
|
||
import goodComposition.Call; | ||
import goodComposition.Money; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class Plan { | ||
private CalculatorBasedOnCalls calculator; | ||
private Set<Call> calls = new HashSet<>(); | ||
|
||
public void setCalculator(final CalculatorBasedOnCalls calculator) { | ||
this.calculator = calculator; | ||
} | ||
|
||
public Money calculateFee() { | ||
return calculator.calculateFee(calls, Money.ZERO); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package practice; | ||
|
||
import goodComposition.Call; | ||
import goodComposition.Money; | ||
import java.util.Set; | ||
|
||
public class SecondStrategy implements CalculatorStrategy { | ||
@Override | ||
public Money calculateFee(final Set<Call> calls, final Money money) { | ||
throw new UnsupportedOperationException("SecondStrategy#calculateFee not implemented."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package practice; | ||
|
||
import goodComposition.Call; | ||
import goodComposition.Money; | ||
import java.util.Set; | ||
|
||
public class ThirdStrategy implements CalculatorStrategy { | ||
@Override | ||
public Money calculateFee(final Set<Call> calls, final Money money) { | ||
throw new UnsupportedOperationException("ThirdStrategy#calculateFee not implemented."); | ||
} | ||
} |