Click here to Skip to main content
12,633,519 members (31,621 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

29.9K views
237 downloads
14 bookmarked
Posted

Refactoring a Switch Statement

, 15 Aug 2009 CPOL
Avoiding a switch statement in order to avoid Cyclomatic complexity.
Refactoring-Switch
Refactoring-Switch
bin
Debug
Refactoring-Switch.exe
Refactoring-Switch.vshost.exe
Properties
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Refactoring_Switch
{
   public class DeviceController
    {
       private DeviceStatesEnum _currentDeviceState = DeviceStatesEnum.ShoutDown;
       private Dictionary<DeviceStatesEnum, Action> _deviceStateHandler = new Dictionary<DeviceStatesEnum, Action>();

       public DeviceController()
       {
           _deviceStateHandler.Add(DeviceStatesEnum.Active, new Action(SetDeviceStateToActive));
           _deviceStateHandler.Add(DeviceStatesEnum.Equilibrating, new Action(SetDeviceStateToEquilibrating));
           _deviceStateHandler.Add(DeviceStatesEnum.Error, new Action(SetDeviceStateToError));
           _deviceStateHandler.Add(DeviceStatesEnum.Inactive, new Action(SetDeviceStateToInactive));
           _deviceStateHandler.Add(DeviceStatesEnum.PowerUp, new Action(SetDeviceStateToPowerUp));
           _deviceStateHandler.Add(DeviceStatesEnum.Ready, new Action(SetDeviceStateToReady));
           _deviceStateHandler.Add(DeviceStatesEnum.Running, new Action(SetDeviceStateToRunning));
           _deviceStateHandler.Add(DeviceStatesEnum.ShoutDown, new Action(SetDeviceStateToShoutDown));
           _deviceStateHandler.Add(DeviceStatesEnum.ShoutingDown, new Action(SetDeviceStateToShoutingDown));
           _deviceStateHandler.Add(DeviceStatesEnum.StartRunning, new Action(SetDeviceStateToActive));
           
           //TODO
           //Create mapping for all the states
       }
       public void ChangeDeviceState(DeviceStatesEnum deviceState)
       {
           _deviceStateHandler[deviceState].Invoke();
       }

       //public void ChangeDeviceState(DeviceStatesEnum deviceState)
       //{
       //    switch (deviceState)
       //    {
       //        case DeviceStatesEnum.Active:
       //             SetDeviceStateToActive();
       //             break;
       //        case DeviceStatesEnum.Equilibrating:
       //             SetDeviceStateToEquilibrating();
       //             break;
       //        case DeviceStatesEnum.Error:
       //             SetDeviceStateToError();
       //             break;
       //        case DeviceStatesEnum.Inactive:
       //             SetDeviceStateToInactive();
       //             break;
       //        case DeviceStatesEnum.PowerUp:
       //             SetDeviceStateToPowerUp();
       //             break;
       //        case DeviceStatesEnum.Ready:
       //             SetDeviceStateToReady();
       //             break;
       //        case DeviceStatesEnum.Running:
       //             SetDeviceStateToRunning();
       //             break;
       //        case DeviceStatesEnum.ShoutDown:
       //             SetDeviceStateToShoutDown();
       //             break;
       //        case DeviceStatesEnum.ShoutingDown:
       //             SetDeviceStateToShoutingDown();
       //             break;
       //        case DeviceStatesEnum.StartRunning:
       //             SetDeviceStateToStartRunning();
       //             break;
              
       //    }
       //}

       public DeviceStatesEnum GetDeviceState()
       {
           return _currentDeviceState;
       }

       private void SetDeviceStateToStartRunning()
       {
           if(_currentDeviceState == DeviceStatesEnum.Ready)
           _currentDeviceState = DeviceStatesEnum.StartRunning;
       }

       private void SetDeviceStateToShoutingDown()
       {
           _currentDeviceState = DeviceStatesEnum.ShoutingDown;
       }

       private void SetDeviceStateToShoutDown()
       {
           _currentDeviceState = DeviceStatesEnum.ShoutDown;
       }

       private void SetDeviceStateToRunning()
       {
           if (_currentDeviceState == DeviceStatesEnum.StartRunning )
           {
               _currentDeviceState = DeviceStatesEnum.Running;
           }
       }

       private void SetDeviceStateToReady()
       {
           if (_currentDeviceState == DeviceStatesEnum.Equilibrating )
           {
               _currentDeviceState = DeviceStatesEnum.Ready;
           }
       }

       private void SetDeviceStateToPowerUp()
       {
           if (_currentDeviceState != DeviceStatesEnum.Error)
               _currentDeviceState = DeviceStatesEnum.PowerUp;
       }

       private void SetDeviceStateToInactive()
       {
           if(_currentDeviceState != DeviceStatesEnum.Error)
           _currentDeviceState = DeviceStatesEnum.Inactive;
       }

       private void SetDeviceStateToError()
       {
           _currentDeviceState = DeviceStatesEnum.Error;
       }

       private void SetDeviceStateToEquilibrating()
       {
           if (_currentDeviceState == DeviceStatesEnum.Active)
           {
               _currentDeviceState = DeviceStatesEnum.Equilibrating;
           }
       }

       private void SetDeviceStateToActive()
       {
           if (_currentDeviceState != DeviceStatesEnum.Error)
           {
               _currentDeviceState = DeviceStatesEnum.Active;
           }
       }
    }
}

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)

Share

About the Author

rajeshjj
Software Developer (Senior) MphasiS an HP company
India India
I am dotnet programmer

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 15 Aug 2009
Article Copyright 2009 by rajeshjj
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid