Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ChainOfResponsibility {
  6. /// <summary>
  7. /// The 'Handler' abstract class
  8. /// </summary>
  9. public abstract class Approver {
  10. public string Id { get; }
  11. protected Approver successor;
  12. protected Approver (string id) {
  13. Id = id;
  14. }
  15. protected Approver () : this ("") { }
  16.  
  17. public void SetSuccessor (Approver approver) {
  18. this.successor = approver;
  19. }
  20.  
  21. public Approver GetSuccessor () => this.successor;
  22. public abstract (bool, ProcessingLevel) ProcessRequest (Purchase purchase);
  23. }
  24. }
  25.  
  26. namespace ChainOfResponsibility {
  27. public class ChainOfResponsibilityManager {
  28. private Dictionary<string, string> relationshipConfig;
  29.  
  30. public ChainOfResponsibilityManager (Dictionary<string, string> relationshipConfig) {
  31. this.relationshipConfig = relationshipConfig;
  32. }
  33. public Approver CreateChainOfApprovers (List<Approver> approvers) {
  34. var configValues = relationshipConfig.Values;
  35. Approver mainApprover = approvers.First (el => !configValues.Contains (el.Id, StringComparer.InvariantCultureIgnoreCase));
  36.  
  37. foreach (var approver in approvers) {
  38. if (relationshipConfig.TryGetValue (approver.Id, out string currentApproverId)) {
  39. var activeApprover = approvers.First (el => el.Id == currentApproverId);
  40. if (activeApprover != null) {
  41. approver.SetSuccessor (activeApprover);
  42. }
  43. }
  44. }
  45.  
  46. return mainApprover;
  47. }
  48. }
  49. }
  50. namespace ChainOfResponsibility {
  51. public class Director : Approver {
  52. public Director (string id) : base (id) { }
  53. public override (bool, ProcessingLevel) ProcessRequest (Purchase purchase) => purchase.Amount < 10 ? (true, ProcessingLevel.Accepted) : successor.ProcessRequest (purchase);
  54. }
  55. }
  56.  
  57. namespace ChainOfResponsibility {
  58. public class President : Approver {
  59. public President (string id) : base (id) { }
  60. public override (bool, ProcessingLevel) ProcessRequest (Purchase purchase) => purchase.Amount < 20 ? (true, ProcessingLevel.EscalationLevelTwo) : (false, ProcessingLevel.Rejected);
  61. }
  62. }
  63. namespace ChainOfResponsibility {
  64. public enum ProcessingLevel {
  65. Accepted,
  66. EscalationLevelOne,
  67. EscalationLevelTwo,
  68. Rejected
  69. }
  70. }
  71.  
  72. namespace ChainOfResponsibility {
  73. public struct Purchase {
  74. public Purchase (double amount, int number, string description) {
  75. Amount = amount;
  76. Number = number;
  77. Description = description;
  78. }
  79.  
  80. public double Amount { get; }
  81. public int Number { get; }
  82. public string Description { get; }
  83.  
  84. public static explicit operator Purchase ((double, int, string) tuple) {
  85. return new Purchase (tuple.Item1, tuple.Item2, tuple.Item3);
  86. }
  87. }
  88. }
  89.  
  90. namespace ChainOfResponsibility {
  91. public class VicePresident : Approver {
  92. public VicePresident (string id) : base (id) { }
  93. public override (bool, ProcessingLevel) ProcessRequest (Purchase purchase) => purchase.Amount < 15 ? (true, ProcessingLevel.EscalationLevelOne) : successor.ProcessRequest (purchase);
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement