Click here to Skip to main content
15,892,737 members
Articles / Programming Languages / C#

The State Design Pattern vs. State Machine

Rate me:
Please Sign up or sign in to vote.
4.62/5 (36 votes)
8 Mar 2013CPOL14 min read 284.2K   75  
How to use the State Design Pattern when compared to State Machines, Switch statements, or If statements.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain
{
    /// <summary>
    /// The Device class is the owner of the different states
    /// that the Device can be in. The Device is alos the 
    /// owner of actions (methods) that can be applied to the
    /// states. In other words, Device is the thing we are 
    /// trying to manipulate through outside behavior.
    /// </summary>
    public class Device : DomainObject
    {
        // Device has a physical door represented by the 
        // Door class.
        private Door _door;

        // Device only knows about generic actions on 
        // certain states. So, we use the base classes of 
        // these states in order execute these commands. 
        // The base classes are abstract classes of the 
        // states.
        private ConfigurationState _configurationState;
        
        // The current mode that the device is in.
        private ModeState _modeState;

        public Device(string name) : base(name)
        {
            Initialize();
        }

        public Device()
        {
            Initialize();
        }

        private void Initialize()
        {
            // We are starting up for the first time.
            _modeState = new ModePowerUpState(this);

            _door = new Door(this);

            // The initial configuration setting for the 
            // device. This initial configuration can come 
            // from an external configuration file, for 
            // example.
            _configurationState = new ProductionConfigurationState(this);

            // The door is initially closed
            _door.DoorState = new DoorClosedState(_door);

            // We are ready
            _modeState.SetModeToIdle();
        }

        public Door Door
        {
            get { return _door; }
            set { _door = value; }
        }

        public ConfigurationState Configuration
        {
            get { return _configurationState; }
            set { _configurationState = value; }
        }

        public ModeState Mode
        {
            get { return _modeState; }
            set { _modeState = value; }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Thomas Jaeger is a Solutions Architect and an industry expert for over 24 years in 10 different industries when it comes to software development in general, cloud-based computing, and iOS development. He is a passionate, fanatic, software designer and creator. He lives against the status quo because, in his opinion, creative and innovative solutions are not created following a linear approach but come, in part, from broad experiences in ones life and from an innovative mind.

Comments and Discussions