Guest User

Untitled

a guest
Dec 10th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. /*
  2. * STATE DERIVED STATUS
  3. * - What is status?
  4. */
  5.  
  6. using System;
  7.  
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine("Creating a new bug report.");
  13.  
  14. var bug = new BugReportContext("Sally Tester");
  15.  
  16. Console.WriteLine(bug.Status);
  17.  
  18. bug.Resolve(ResolvedReason.Fixed);
  19.  
  20. Console.WriteLine(bug.Status);
  21.  
  22. bug.Close(ClosedReason.Verified);
  23.  
  24. Console.WriteLine(bug.Status);
  25.  
  26. Console.WriteLine("Creating a new bug report.");
  27.  
  28. // Second round...
  29.  
  30. bug = new BugReportContext("Sally Tester");
  31.  
  32. Console.WriteLine(bug.Status);
  33.  
  34. bug.Resolve(ResolvedReason.NotABug);
  35.  
  36. Console.WriteLine(bug.Status);
  37.  
  38. Console.WriteLine("Reopening the bug report.");
  39.  
  40. bug.Reopen();
  41.  
  42. Console.WriteLine(bug.Status);
  43.  
  44. bug.Resolve(ResolvedReason.Fixed);
  45.  
  46. Console.WriteLine(bug.Status);
  47.  
  48. bug.Close(ClosedReason.Verified);
  49.  
  50. Console.WriteLine(bug.Status);
  51. }
  52. }
  53.  
  54. public enum ResolvedReason
  55. {
  56. Fixed,
  57. NotABug,
  58. Duplicate
  59. }
  60.  
  61. public enum ClosedReason
  62. {
  63. Verified,
  64. NotABug,
  65. Duplicate
  66. }
  67.  
  68. public class BugReportContext
  69. {
  70. public BugReportContext(string submitter)
  71. {
  72. this.Submitter = submitter;
  73. this.CurrentState = new OpenState();
  74. }
  75.  
  76. public IState CurrentState { get; set; }
  77.  
  78. public ResolvedReason? ResolvedReason { get; set; }
  79.  
  80. public ClosedReason? ClosedReason { get; set; }
  81.  
  82. public string Status
  83. {
  84. get
  85. {
  86. return this.CurrentState.Status(this);
  87. }
  88. }
  89.  
  90. public string Owner { get; set; }
  91.  
  92. public string Submitter { get; set; }
  93.  
  94. public void Assign(string owner)
  95. {
  96. this.Owner = owner;
  97. }
  98.  
  99. public void Resolve(ResolvedReason reason)
  100. {
  101. this.CurrentState.Resolve(this, reason);
  102. }
  103.  
  104. public void Reopen()
  105. {
  106. this.CurrentState.Reopen(this);
  107. }
  108.  
  109. public void Close(ClosedReason reason)
  110. {
  111. this.CurrentState.Close(this, reason);
  112. }
  113. }
  114.  
  115. public interface IState
  116. {
  117. void Resolve(BugReportContext context, ResolvedReason reason);
  118. void Reopen(BugReportContext context);
  119. void Close(BugReportContext context, ClosedReason reason);
  120. string Status(BugReportContext context);
  121. }
  122.  
  123. public class OpenState : IState
  124. {
  125. public void Resolve(BugReportContext context, ResolvedReason reason)
  126. {
  127. context.Owner = context.Submitter;
  128. context.ResolvedReason = reason;
  129. context.CurrentState = new ResolvedState();
  130. }
  131.  
  132. public void Reopen(BugReportContext context)
  133. {
  134. return;
  135. }
  136.  
  137. public void Close(BugReportContext context, ClosedReason reason)
  138. {
  139. if (reason == ClosedReason.Verified)
  140. {
  141. Console.WriteLine("You cannot verify and close a bug report that has not been resolved!");
  142. return;
  143. }
  144.  
  145. context.ClosedReason = reason;
  146. context.CurrentState = new ResolvedState();
  147. }
  148.  
  149. public string Status(BugReportContext context)
  150. {
  151. if (string.IsNullOrEmpty(context.Owner))
  152. {
  153. return "The bug report is open and unassigned.";
  154. }
  155.  
  156. return string.Format("The bug report is open and assigned to {0}.", context.Owner);
  157. }
  158. }
  159.  
  160. public class ResolvedState : IState
  161. {
  162. public void Resolve(BugReportContext context, ResolvedReason reason)
  163. {
  164. return;
  165. }
  166.  
  167. public void Reopen(BugReportContext context)
  168. {
  169. context.Owner = null;
  170. context.ResolvedReason = null;
  171. context.CurrentState = new OpenState();
  172. }
  173.  
  174. public void Close(BugReportContext context, ClosedReason reason)
  175. {
  176. if (reason == ClosedReason.Verified && context.ResolvedReason != ResolvedReason.Fixed)
  177. {
  178. Console.WriteLine("You cannot verify and close a bug report that has not been fixed!");
  179. return;
  180. }
  181.  
  182. context.ClosedReason = reason;
  183. context.CurrentState = new ClosedState();
  184. }
  185.  
  186. public string Status(BugReportContext context)
  187. {
  188. return string.Format("The bug report is resolved as {0} and assigned to {0}.", context.ResolvedReason, context.Owner);
  189. }
  190. }
  191.  
  192. public class ClosedState : IState
  193. {
  194. public void Resolve(BugReportContext context, ResolvedReason reason)
  195. {
  196. Console.WriteLine("You cannot resolve a bug report that has been closed!");
  197. }
  198.  
  199. public void Reopen(BugReportContext context)
  200. {
  201. context.ResolvedReason = null;
  202. context.ClosedReason = null;
  203. context.CurrentState = new OpenState();
  204. }
  205.  
  206. public void Close(BugReportContext context, ClosedReason reason)
  207. {
  208. return;
  209. }
  210.  
  211. public string Status(BugReportContext context)
  212. {
  213. return string.Format("The bug report is closed because it was {0}.", context.ClosedReason);
  214. }
  215. }
Add Comment
Please, Sign In to add comment