Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //A very simple model of Order, I will not include any implementation here
- public interface Order {
- String orderID ();
- String item ();
- int quantity ();
- double price ();
- String shipmentAddress ();
- }
- /**
- * A Rule is the actual logic of a checker.
- * It runs against the given Order, using the given RuleParameter
- * and reports its result to RuleVisitor
- */
- public interface Rule {
- void run (Order order, RuleParameter parameter, RuleVisitor visitor);
- }
- //The parameter of a Rule that is provided from external configuration
- public interface RuleParameter {
- double warnLimit ();
- double errorLimit ();
- }
- public interface RuleVisitor {
- void error (String reason);
- void warning (String reason);
- void allGood ();
- }
- /**
- * Context is the proxy between the validation framework and
- * the outside world. The major responsibility is to capture
- * all the information required and show it to user
- */
- public interface Context {
- void warn (String orderID, Checker checker, String reason);
- void error (String orderID, Checker checker, String reason);
- void allGood (String orderID, Checker checker);
- }
- /**
- * The main class to layout the validation flow
- */
- public class OrderManager {
- private final Context context = new Context () {
- @Override
- public void warn(String orderID, Checker checker, String reason) {
- //There will be a lot of stuff in reality, building warning object,
- //rendering, caching warning/errors etc.
- System.out.println(String.format (
- "%s has raised a warning for order %s. Reason: %s",
- checker, orderID, reason));
- }
- @Override
- public void error(String orderID, Checker checker, String reason) { }
- @Override
- public void allGood(String orderID, Checker checker) { }
- };
- public void process (Collection<Order> orders) {
- for (Order order : orders) {
- Collection<Checker> checkers = getEnabledCheckers (order);
- for (Checker checker : checkers) {
- RuleParameter parameter = getParameter (order, checker);
- checker.check(order, parameter, context);
- }
- }
- }
- public Collection<Checker> getEnabledCheckers (Order order) {
- //the list of checker names applicable, should come from config
- Collection <String> enabledCheckerNames =
- Arrays.asList("OrderValueChecker", "QuantityChecker");
- Collection <Checker> checkers = new ArrayList<> ();
- for (String name : enabledCheckerNames) {
- checkers.add(Checker.valueOf(name));
- }
- return checkers;
- }
- public RuleParameter getParameter (Order order, Checker checker) {
- //again, should be coming from external configuration
- return null;
- }
- }
- /**
- * The definition of all available checkers. A Checker
- * is a warp of a Rule instance. The actually validation
- * logic is in the Rule instead of Checker.
- * Checker is defined to name the available Rules in a
- * cleaner manner
- */
- public enum Checker {
- OrderValueChecker (new OrderValueRule ()),
- QuantityChecker (new QuantityRule ());
- private final Rule rule;
- Checker (Rule rule) {
- this.rule = rule;
- }
- public void check(final Order order, final RuleParameter parameter, final Context context) {
- //visitor is to direct the Rule result to the given
- //context with enriched information, such as Checker and orderID
- RuleVisitor visitor = new RuleVisitor () {
- @Override
- public void error(String reason) {
- context.error(order.orderID(), Checker.this, reason);
- }
- @Override
- public void warning(String reason) {
- context.warn(order.orderID(), Checker.this, reason);
- }
- @Override
- public void allGood() {
- context.allGood(order.orderID(), Checker.this);
- }
- };
- rule.run(order, parameter, visitor);
- }
- }
- /**
- * A demo implementation of a Rule to examine if an order is too valuable.
- * If it is a very valuable order, warn the user to add an insurance
- * If it is an extremely valuable order, block the user from placing it
- */
- public class OrderValueRule implements Rule {
- @Override
- public void run (Order order, RuleParameter parameter, RuleVisitor visitor) {
- double value = order.quantity() * order.price ();
- final double limitForError = parameter.errorLimit();
- if (value > limitForError) {
- visitor.error(String.format(errorReasonTemplate, limitForError));
- return;
- }
- final double limitForWarning = parameter.warnLimit();
- if (value > limitForWarning) {
- visitor.warning(String.format(warningReasonTemplate, limitForWarning));
- return;
- }
- visitor.allGood();
- }
- private final String errorReasonTemplate = "Order of value larger than %f is not allowed";
- private final String warningReasonTemplate =
- "Order value is larger than %f. Please consider adding an insurance for shipment";
- }
- public class QuantityRule implements Rule {
- //implementation omitted
- ...
- }
- double warnLimit ();
- public Collection<Checker> getEnabledCheckers (Order order) {
- //the list of checker names applicable, should come from config
- Collection <String> enabledCheckerNames =
- Arrays.asList("OrderValueChecker", "QuantityChecker");
- public enum Checker {
- OrderValueChecker (new OrderValueRule ()),
- public interface Context {
- void warn (String orderID, Checker checker, String reason);
- void error (String orderID, Checker checker, String reason);
- void allGood (String orderID, Checker checker);
- }
- public interface RuleVisitor {
- void error (String reason);
- void warning (String reason);
- void allGood ();
- }
- public abstract void check(final Order order, final RuleParameter parameter, final Context context);
- final double limitForError = parameter.errorLimit();
- if (value > limitForError) {
- visitor.error(String.format(errorReasonTemplate, limitForError));
- return;
- }
- if (value > parameter.errorLimit()) {
- visitor.error(String.format(errorReasonTemplate, parameter.errorLimit()));
- } else if ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement