Jul 13 2015

Implementing Enums on steroids with a variation of the Flyweight pattern

My friend Chris McKenzie keyed me onto this pattern.  Very cool.   (I keep thinking the pattern is called Flywheel.  But it is Flyweight. )

I am copying the entire code block here in case it ever goes away since he has moved the source blog post.  In case it doesn’t here is the link to the original post.

https://crmckenzie.wordpress.com/2011/04/19/smart-enumerations-or-using-flyweight-to-beat-enums-into-submission/

And here is the code.

public sealed class TaskState
{
    public static TaskState Pending { get; private set; }
    public static TaskState InProgress { get; private set; }
    public static TaskState Completed { get; private set;}
    public static TaskState Deferred { get; private set; }
    public static TaskState Canceled { get; private set; }

    public string Name { get; private set; }
    public string Value { get; private set; }

    private readonly List<TaskState> _transitions = new List<TaskState>();

    private TaskState AddTransition(TaskState value)
    {
        this._transitions.Add(value);
        return this;
    }

    public bool CanTransitionTo(TaskState value)
    {
        return this._transitions.Contains(value);
    }

    private TaskState()
    {
        
    }

    static TaskState()
    {
        BuildStates();
        ConfigureTransitions();
    }

    private static void ConfigureTransitions()
    {
        Pending.AddTransition(InProgress).AddTransition(Canceled);
        InProgress.AddTransition(Completed).AddTransition(Deferred).AddTransition(Canceled);
        Deferred.AddTransition(InProgress).AddTransition(Canceled);
    }

    private static void BuildStates()
    {
        Pending = new TaskState()
                      {
                          Name = "Pending",
                          Value = "Pending",
                      };
        InProgress = new TaskState()
                         {
                             Name = "In Progress",
                             Value = "InProgress",
                         };
        Completed = new TaskState()
                        {
                            Name = "Completed",
                            Value = "Completed",
                        };
        Deferred = new TaskState()
                       {
                           Name = "Deferred",
                           Value = "Deferred",
                       };
        Canceled = new TaskState()
                       {
                           Name = "Canceled",
                           Value = "Canceled",
                       };
    }
}

And the usage…

var task = new Task()
               {
                   State = TaskState.Pending,
               };

if (task.State.CanTransitionTo(TaskState.Completed))
{
    // do something
}
Jul 01 2015

Comment by Seth Spearman on How to schedule a task to run when shutting down windows

This is the better answer. When you open GPEditor it shows two nodes as you describe. Since both nodes have a Windows Settings option then the accepted answer is less clear than your answer. Thanks for the tip. Very helpful.
Jul 01 2015

Comment by Seth Spearman on How to schedule a task to run when shutting down windows

This is the better answer. When you open GPEditor it shows two nodes as you describe. Since both nodes have a Windows Settings option then the accepted answer is less clear than your answer. Thanks for the tip. Very helpful.