Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * STATE DERIVED STATUS
- * - What is status?
- */
- using System;
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Creating a new bug report.");
- var bug = new BugReportContext("Sally Tester");
- Console.WriteLine(bug.Status);
- bug.Resolve(ResolvedReason.Fixed);
- Console.WriteLine(bug.Status);
- bug.Close(ClosedReason.Verified);
- Console.WriteLine(bug.Status);
- Console.WriteLine("Creating a new bug report.");
- // Second round...
- bug = new BugReportContext("Sally Tester");
- Console.WriteLine(bug.Status);
- bug.Resolve(ResolvedReason.NotABug);
- Console.WriteLine(bug.Status);
- Console.WriteLine("Reopening the bug report.");
- bug.Reopen();
- Console.WriteLine(bug.Status);
- bug.Resolve(ResolvedReason.Fixed);
- Console.WriteLine(bug.Status);
- bug.Close(ClosedReason.Verified);
- Console.WriteLine(bug.Status);
- }
- }
- public enum ResolvedReason
- {
- Fixed,
- NotABug,
- Duplicate
- }
- public enum ClosedReason
- {
- Verified,
- NotABug,
- Duplicate
- }
- public class BugReportContext
- {
- public BugReportContext(string submitter)
- {
- this.Submitter = submitter;
- this.CurrentState = new OpenState();
- }
- public IState CurrentState { get; set; }
- public ResolvedReason? ResolvedReason { get; set; }
- public ClosedReason? ClosedReason { get; set; }
- public string Status
- {
- get
- {
- return this.CurrentState.Status(this);
- }
- }
- public string Owner { get; set; }
- public string Submitter { get; set; }
- public void Assign(string owner)
- {
- this.Owner = owner;
- }
- public void Resolve(ResolvedReason reason)
- {
- this.CurrentState.Resolve(this, reason);
- }
- public void Reopen()
- {
- this.CurrentState.Reopen(this);
- }
- public void Close(ClosedReason reason)
- {
- this.CurrentState.Close(this, reason);
- }
- }
- public interface IState
- {
- void Resolve(BugReportContext context, ResolvedReason reason);
- void Reopen(BugReportContext context);
- void Close(BugReportContext context, ClosedReason reason);
- string Status(BugReportContext context);
- }
- public class OpenState : IState
- {
- public void Resolve(BugReportContext context, ResolvedReason reason)
- {
- context.Owner = context.Submitter;
- context.ResolvedReason = reason;
- context.CurrentState = new ResolvedState();
- }
- public void Reopen(BugReportContext context)
- {
- return;
- }
- public void Close(BugReportContext context, ClosedReason reason)
- {
- if (reason == ClosedReason.Verified)
- {
- Console.WriteLine("You cannot verify and close a bug report that has not been resolved!");
- return;
- }
- context.ClosedReason = reason;
- context.CurrentState = new ResolvedState();
- }
- public string Status(BugReportContext context)
- {
- if (string.IsNullOrEmpty(context.Owner))
- {
- return "The bug report is open and unassigned.";
- }
- return string.Format("The bug report is open and assigned to {0}.", context.Owner);
- }
- }
- public class ResolvedState : IState
- {
- public void Resolve(BugReportContext context, ResolvedReason reason)
- {
- return;
- }
- public void Reopen(BugReportContext context)
- {
- context.Owner = null;
- context.ResolvedReason = null;
- context.CurrentState = new OpenState();
- }
- public void Close(BugReportContext context, ClosedReason reason)
- {
- if (reason == ClosedReason.Verified && context.ResolvedReason != ResolvedReason.Fixed)
- {
- Console.WriteLine("You cannot verify and close a bug report that has not been fixed!");
- return;
- }
- context.ClosedReason = reason;
- context.CurrentState = new ClosedState();
- }
- public string Status(BugReportContext context)
- {
- return string.Format("The bug report is resolved as {0} and assigned to {0}.", context.ResolvedReason, context.Owner);
- }
- }
- public class ClosedState : IState
- {
- public void Resolve(BugReportContext context, ResolvedReason reason)
- {
- Console.WriteLine("You cannot resolve a bug report that has been closed!");
- }
- public void Reopen(BugReportContext context)
- {
- context.ResolvedReason = null;
- context.ClosedReason = null;
- context.CurrentState = new OpenState();
- }
- public void Close(BugReportContext context, ClosedReason reason)
- {
- return;
- }
- public string Status(BugReportContext context)
- {
- return string.Format("The bug report is closed because it was {0}.", context.ClosedReason);
- }
- }
Add Comment
Please, Sign In to add comment