StateFactories.cs 3.1 KB

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