StateFactories.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections.Generic;
  2. namespace Wayne.Lib.StateEngine
  3. {
  4. /// <summary>
  5. /// A collection of State factories. A state machine can have states created by multiple state factories.
  6. /// When a state object should be created, the factories are queried one by one for the state name.
  7. /// </summary>
  8. public class StateFactories
  9. {
  10. #region Fields
  11. private List<IStateFactory> factoryList = new List<IStateFactory>();
  12. #endregion
  13. #region Construction
  14. /// <summary>
  15. /// Constructor
  16. /// </summary>
  17. public StateFactories()
  18. {
  19. }
  20. #endregion
  21. #region Public Methods
  22. /// <summary>
  23. /// Clears the list.
  24. /// </summary>
  25. public void Clear()
  26. {
  27. factoryList.Clear();
  28. }
  29. /// <summary>
  30. /// Add a factory to the StateFactory Collection.
  31. /// </summary>
  32. /// <param name="stateFactory">
  33. /// The factory that should be added.
  34. /// </param>
  35. public void AddFactory(IStateFactory stateFactory)
  36. {
  37. if ((stateFactory != null) && !factoryList.Contains(stateFactory))
  38. factoryList.Add(stateFactory);
  39. }
  40. /// <summary>
  41. /// Calls each registered State Factory to create the requested State.
  42. /// If more than one factory returns a state with the specified name,
  43. /// the method will throw an exception.
  44. ///
  45. /// If no factory returns a state, null will be returned.
  46. ///
  47. /// If exactly one state was created, it will be returned.
  48. /// </summary>
  49. /// <param name="stateFactoryName">Name of the State that is going to be created.</param>
  50. ///<param name="stateTypeContainer"></param>
  51. ///<returns>The created state</returns>
  52. /// <exception cref="StateEngineException">If more than one factory creates the state, an exception will be thrown.</exception>
  53. public State CreateState(string stateFactoryName, StateTypeContainer stateTypeContainer)
  54. {
  55. State foundState = null;
  56. //Ask each factory to create the state object.
  57. foreach (IStateFactory factory in factoryList)
  58. {
  59. IStateFactory2 stateFactory2 = factory as IStateFactory2;
  60. State state;
  61. if (stateFactory2 != null)
  62. state = stateFactory2.CreateState(stateFactoryName, stateTypeContainer);
  63. else
  64. state = factory.CreateState(stateFactoryName);
  65. if (state != null)
  66. {
  67. foundState = state;
  68. break;
  69. }
  70. }
  71. CompositeState compositeState = foundState as CompositeState;
  72. if (compositeState != null)
  73. {
  74. compositeState.StateMachine.stateFactories = this;
  75. compositeState.StateMachine.SetAndMergeTypeContainer(stateTypeContainer);
  76. }
  77. return foundState;
  78. }
  79. #endregion
  80. }
  81. }