Simple state machine (workflow)

Got sick of WWF, made this, any good?

Page 1 of 1

3 Replies - 12593 Views - Last Post: 01 June 2010 - 02:57 PM

#1 oldSwede   User is offline

  • D.I.C Regular
  • member icon

Reputation: 4
  • View blog
  • Posts: 464
  • Joined: 08-January 16

Simple state machine (workflow)

Posted 30 May 2010 - 04:11 PM

Hi!

At work I am forced to use a state machine and a collegue of mine wants me to use WWF (Windows workflow foundation) which I really find to be a mess. Since the flow, or rather the state machine, that we need is pretty simple I thought I'd try to come up with somthing simpler. I figured that I might be able to change their minds at work by suggesting something along the lines of the ZIP attached to this post.

Any code comments and/or improvements will be appreciated. However, if you descide to improve the code I ask you to keep things simple - I want this to be useful to C# / .NET -beginners such as myself. :)

Regards
Jens

Attached File  JensStateMachine1.zip (47.29K)
Number of downloads: 422

PS: I thought this was the place to post the code since I don't actually ask for help and the subject isn't really for beginners.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple state machine (workflow)

#2 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: Simple state machine (workflow)

Posted 01 June 2010 - 02:26 AM

When it's decided that you should use a "state machine", this term is usually designated to actually mean the implementation of the design pattern. What you have here is not that. What you have is a form very tightly coupled to a "Business" class that has the knowledge of every state it can be in.


What a state machine really is is an abstraction of several states under the guise of a single context. This means that at different moments in object's life-cycle, it represents different things.


So, I think that a stop light is a pretty good idea behind a state machine. The light goes from green to yellow to red to green. This means that a state of "red" knows that the transition is "green" and a state of "green" knows that the transition is "yellow".


In terms of actual implementation, each light could be considered an ILightState. It might have a single method called "Change()" which takes the existing state and changes it. Let me give you a full implementation. You can compare it against what you have and see how different the concepts really are.


using System;

namespace TrafficLightStateMachineDemo
{
    /// <summary>
    /// This represents a possible state in the life-cycle of a traffic light.
    /// </summary>
    interface ITrafficLightState
    {
        // This provides the ability for a traffic light to change its state:
        void Change(TrafficLight light);

        // We will know what state we're in by reporting it:
        void ReportState();
    }

    /// <summary>
    /// This is the state of being set to "green" for our traffic light.
    /// </summary>
    class GreenLight : ITrafficLightState
    {
        // We pass in the light and with that, green knows to change it to yellow.
        public void Change(TrafficLight light)
        {
            light.State = new YellowLight();
        }

        public void ReportState()
        {
            Console.WriteLine("Green Light");
        }
    }

    /// <summary>
    /// This is the state of being "yellow" for our traffic light.
    /// </summary>
    class YellowLight : ITrafficLightState
    {
        // A yellow light knows to change it to red.
        public void Change(TrafficLight light)
        {
            light.State = new RedLight();
        }

        public void ReportState()
        {
            Console.WriteLine("Yellow Light");
        }
    }

    /// <summary>
    /// This is the state of being "red" for our traffic light.
    /// </summary>
    class RedLight : ITrafficLightState
    {
        // A red light knows to change it back to green.
        public void Change(TrafficLight light)
        {
            light.State = new GreenLight();
        }

        public void ReportState()
        {
            Console.WriteLine("Red Light");
        }
    }

    /// <summary>
    /// This is the "thing" that changes its state. It maintains a state which is publicly changeable.
    /// </summary>
    class TrafficLight
    {
        /// <summary>
        /// This is the state that our traffic light maintains.
        /// </summary>
        public ITrafficLightState State
        { get; set; }

        // We pass in the initial state via the constructor.
        public TrafficLight(ITrafficLightState startingState)
        {
            State = startingState;
        }

        // This simply calls on its currently stored state's Change method. Whatever real implementation it is
        // will change the state to the correct one in the chain.
        public void Change()
        {
            State.Change(this);
        }

        // Now we can report what state it's currently in by asking the state to report for us:
        public void ReportState()
        {
            State.ReportState();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // To test, we create a traffic light and start its state at Red:
            TrafficLight light = new TrafficLight(new RedLight());

            // Now we can run a loop to change it around:
            for (int count = 0; count <= 11; count++)
            {
                // Notice that calling code has no idea what the hell is going to happen.
                // Ultimately, the traffic light doesn't really know either.
                // In fact, we can add even more states without the traffic light breaking...
                // as long as those new state implement ITrafficLightState and some existing state
                // knows how to get to this new state.
                light.Change();
                light.ReportState();
            }

            Console.ReadLine();
        }
    }
}



Green Light
Yellow Light
Red Light
Green Light
Yellow Light
Red Light
Green Light
Yellow Light
Red Light
Green Light
Yellow Light
Red Light



THAT is a state machine.

This post has been edited by MentalFloss: 01 June 2010 - 02:31 AM

Was This Post Helpful? 2
  • +
  • -

#3 oldSwede   User is offline

  • D.I.C Regular
  • member icon

Reputation: 4
  • View blog
  • Posts: 464
  • Joined: 08-January 16

Re: Simple state machine (workflow)

Posted 01 June 2010 - 12:50 PM

Aoowww... That felt a little harsh. :)

My collegue (who descides) really wants me to implement the .NET 3.5 WWF state machine even though not needed (I think). I find the WWF to be really bloated and excessivly complex for the problem at hand. I need something simple with few "advanced" C# features such as e.g. Interfaces, Delegates or reflection. People with even less skill than mine will take over this code when it's done so it needs to be very straight forward.

So, lets assume that the badly named class "Business" in my code is there to work as an interface between the "state machine" and the rest of the program. Still worthless?

I'll redesign a bit and post again.

Thanks for your time.
Jens

This post has been edited by jens: 01 June 2010 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: Simple state machine (workflow)

Posted 01 June 2010 - 02:57 PM

I think you would benefit from giving us specifics of the project. With this information it will be easier to guide you to a solution.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1