Last active
July 8, 2023 03:59
-
-
Save AutMaple/11c6e01f6eb06e6800e1194352d6e07a to your computer and use it in GitHub Desktop.
[规格模式] #设计模式#
This file contains hidden or 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
| @RequiredArgsConstructor | |
| public class AndExpression extends CompositeExpression { | |
| private final Expression left; | |
| private final Expression right; | |
| @Override | |
| public boolean compute(Map<String, Double> data) { | |
| return left.compute(data) && right.compute(data); | |
| } | |
| } |
This file contains hidden or 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 abstract class CompositeExpression implements Expression { | |
| @Override | |
| public Expression and(Expression a) { | |
| return new AndExpression(this, a); | |
| } | |
| @Override | |
| public Expression or(Expression a) { | |
| return new OrExpression(this, a); | |
| } | |
| @Override | |
| public Expression not(Expression a) { | |
| return new NotExpression(a); | |
| } | |
| } |
This file contains hidden or 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
| @RequiredArgsConstructor | |
| public class EqualExpression extends CompositeExpression { | |
| private final String indicatorName; | |
| private final double presetValue; | |
| @Override | |
| public boolean compute(Map<String, Double> data) { | |
| Double value = data.get(indicatorName); | |
| if (value == null) | |
| return true; | |
| return this.presetValue == value; | |
| } | |
| } |
This file contains hidden or 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 interface Expression { | |
| boolean compute(Map<String, Double> value); | |
| Expression and(Expression a); | |
| Expression or(Expression a); | |
| Expression not(Expression a); | |
| } |
This file contains hidden or 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
| @RequiredArgsConstructor | |
| public class NotExpression extends CompositeExpression { | |
| private final Expression expression; | |
| @Override | |
| public boolean compute(Map<String, Double> data) { | |
| return !expression.compute(data); | |
| } | |
| } |
This file contains hidden or 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
| @RequiredArgsConstructor | |
| public class OrExpression extends CompositeExpression { | |
| private final Expression left; | |
| private final Expression right; | |
| @Override | |
| public boolean compute(Map<String, Double> data) { | |
| return left.compute(data) || right.compute(data); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment