Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. //A very simple model of Order, I will not include any implementation here
  2. public interface Order {
  3. String orderID ();
  4. String item ();
  5. int quantity ();
  6. double price ();
  7. String shipmentAddress ();
  8. }
  9.  
  10. /**
  11. * A Rule is the actual logic of a checker.
  12. * It runs against the given Order, using the given RuleParameter
  13. * and reports its result to RuleVisitor
  14. */
  15. public interface Rule {
  16. void run (Order order, RuleParameter parameter, RuleVisitor visitor);
  17. }
  18.  
  19. //The parameter of a Rule that is provided from external configuration
  20. public interface RuleParameter {
  21. double warnLimit ();
  22. double errorLimit ();
  23. }
  24.  
  25. public interface RuleVisitor {
  26. void error (String reason);
  27. void warning (String reason);
  28. void allGood ();
  29. }
  30.  
  31. /**
  32. * Context is the proxy between the validation framework and
  33. * the outside world. The major responsibility is to capture
  34. * all the information required and show it to user
  35. */
  36. public interface Context {
  37. void warn (String orderID, Checker checker, String reason);
  38. void error (String orderID, Checker checker, String reason);
  39. void allGood (String orderID, Checker checker);
  40. }
  41.  
  42. /**
  43. * The main class to layout the validation flow
  44. */
  45. public class OrderManager {
  46. private final Context context = new Context () {
  47. @Override
  48. public void warn(String orderID, Checker checker, String reason) {
  49. //There will be a lot of stuff in reality, building warning object,
  50. //rendering, caching warning/errors etc.
  51. System.out.println(String.format (
  52. "%s has raised a warning for order %s. Reason: %s",
  53. checker, orderID, reason));
  54. }
  55. @Override
  56. public void error(String orderID, Checker checker, String reason) { }
  57. @Override
  58. public void allGood(String orderID, Checker checker) { }
  59. };
  60.  
  61. public void process (Collection<Order> orders) {
  62. for (Order order : orders) {
  63. Collection<Checker> checkers = getEnabledCheckers (order);
  64. for (Checker checker : checkers) {
  65. RuleParameter parameter = getParameter (order, checker);
  66. checker.check(order, parameter, context);
  67. }
  68. }
  69. }
  70.  
  71. public Collection<Checker> getEnabledCheckers (Order order) {
  72. //the list of checker names applicable, should come from config
  73. Collection <String> enabledCheckerNames =
  74. Arrays.asList("OrderValueChecker", "QuantityChecker");
  75. Collection <Checker> checkers = new ArrayList<> ();
  76. for (String name : enabledCheckerNames) {
  77. checkers.add(Checker.valueOf(name));
  78. }
  79. return checkers;
  80. }
  81.  
  82. public RuleParameter getParameter (Order order, Checker checker) {
  83. //again, should be coming from external configuration
  84. return null;
  85. }
  86. }
  87.  
  88. /**
  89. * The definition of all available checkers. A Checker
  90. * is a warp of a Rule instance. The actually validation
  91. * logic is in the Rule instead of Checker.
  92. * Checker is defined to name the available Rules in a
  93. * cleaner manner
  94. */
  95. public enum Checker {
  96. OrderValueChecker (new OrderValueRule ()),
  97. QuantityChecker (new QuantityRule ());
  98.  
  99. private final Rule rule;
  100.  
  101. Checker (Rule rule) {
  102. this.rule = rule;
  103. }
  104. public void check(final Order order, final RuleParameter parameter, final Context context) {
  105. //visitor is to direct the Rule result to the given
  106. //context with enriched information, such as Checker and orderID
  107. RuleVisitor visitor = new RuleVisitor () {
  108. @Override
  109. public void error(String reason) {
  110. context.error(order.orderID(), Checker.this, reason);
  111. }
  112. @Override
  113. public void warning(String reason) {
  114. context.warn(order.orderID(), Checker.this, reason);
  115. }
  116. @Override
  117. public void allGood() {
  118. context.allGood(order.orderID(), Checker.this);
  119. }
  120. };
  121.  
  122. rule.run(order, parameter, visitor);
  123. }
  124. }
  125.  
  126. /**
  127. * A demo implementation of a Rule to examine if an order is too valuable.
  128. * If it is a very valuable order, warn the user to add an insurance
  129. * If it is an extremely valuable order, block the user from placing it
  130. */
  131. public class OrderValueRule implements Rule {
  132. @Override
  133. public void run (Order order, RuleParameter parameter, RuleVisitor visitor) {
  134. double value = order.quantity() * order.price ();
  135.  
  136. final double limitForError = parameter.errorLimit();
  137. if (value > limitForError) {
  138. visitor.error(String.format(errorReasonTemplate, limitForError));
  139. return;
  140. }
  141.  
  142. final double limitForWarning = parameter.warnLimit();
  143. if (value > limitForWarning) {
  144. visitor.warning(String.format(warningReasonTemplate, limitForWarning));
  145. return;
  146. }
  147.  
  148. visitor.allGood();
  149. }
  150.  
  151. private final String errorReasonTemplate = "Order of value larger than %f is not allowed";
  152. private final String warningReasonTemplate =
  153. "Order value is larger than %f. Please consider adding an insurance for shipment";
  154. }
  155.  
  156. public class QuantityRule implements Rule {
  157. //implementation omitted
  158. ...
  159. }
  160.  
  161. double warnLimit ();
  162.  
  163. public Collection<Checker> getEnabledCheckers (Order order) {
  164. //the list of checker names applicable, should come from config
  165. Collection <String> enabledCheckerNames =
  166. Arrays.asList("OrderValueChecker", "QuantityChecker");
  167.  
  168. public enum Checker {
  169. OrderValueChecker (new OrderValueRule ()),
  170.  
  171. public interface Context {
  172. void warn (String orderID, Checker checker, String reason);
  173. void error (String orderID, Checker checker, String reason);
  174. void allGood (String orderID, Checker checker);
  175. }
  176.  
  177. public interface RuleVisitor {
  178. void error (String reason);
  179. void warning (String reason);
  180. void allGood ();
  181. }
  182.  
  183. public abstract void check(final Order order, final RuleParameter parameter, final Context context);
  184.  
  185. final double limitForError = parameter.errorLimit();
  186. if (value > limitForError) {
  187. visitor.error(String.format(errorReasonTemplate, limitForError));
  188. return;
  189. }
  190.  
  191. if (value > parameter.errorLimit()) {
  192. visitor.error(String.format(errorReasonTemplate, parameter.errorLimit()));
  193. } else if ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement