Last active
January 8, 2021 14:55
-
-
Save wooklab/3bb2b0d890c690443259bc675e8e60bb to your computer and use it in GitHub Desktop.
How to Replace Many if Statements in Java 에서 3.4. Rule Engine 적용버전
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
/** | |
* How to Replace Many if Statements in Java(https://www.baeldung.com/java-replace-if-statements) | |
* - 3.4. Rule Engine 적용버전 | |
*/ | |
public interface Policy<T, R> { | |
boolean evaluate(T t, PolicyConditions policyConditions); | |
R apply(T t, PolicyConditions policyConditions); | |
} |
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
public class SomePolicy implements Policy<SomePolicyHelper, SomeResult> { | |
@Override | |
public boolean evaluate(SomePolicyHelper somePolicyHelper, PolicyConditions policyConditions) { | |
// return 해당 정책 적용여부 | |
} | |
@Override | |
public SomeResult apply(SomePolicyHelper somePolicyHelper, PolicyConditions policyConditions) { | |
// return 정책 반영 결과 | |
} | |
} |
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
public class PriceInfoProvider { | |
private final List<Policy> policies; | |
public SomeResult provide(SomePolicyHelper somePolicyHelper, PolicyConditions policyConditions) { | |
PriceInfoPolicy priceInfoPolicy = policies | |
.stream() | |
.filter(policy -> policy.evaluate(somePolicyHelper, policyConditions)) | |
.findFirst() | |
.orElseThrow(() -> new RuntimeException(EXCEPTION_MESSAGE)); | |
return priceInfoPolicy.apply(somePolicyHelper, policyConditions); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment