Advertisement
apieceoffruit

Untitled

May 5th, 2021 (edited)
873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1.  
  2.  
  3. public class StateFactory
  4. {
  5.    GameElementData _data;
  6.    public StateFactory(GameElementData data) => _data = data;
  7.  
  8.    public State CreateStateA() => new StateA(_data);
  9.    public State CreateStateB() => new StateB(_data);
  10.    public State CreateStateC() => new StateC(_data);
  11.    public State CreateStateD() => new StateD(_data);
  12.  
  13. }
  14.  
  15. public abstract class State
  16. {
  17.    void Enter();
  18.    void Update();
  19. }
  20.  
  21.  
  22. class UsageExample : MonoBehaviour
  23. {
  24.    StateFactory _states;
  25.  
  26.    State _current;
  27.  
  28.    public void Awake()
  29.   {
  30.       _states = new StateFactory(new GameElemntDAta());
  31.  
  32.     _Current = _states.CreateStateA(); // etc
  33. }
  34.  
  35.  
  36. }
  37.  
  38. // now imagine adding an extra bit of data to all states, it has one place to do it, in the factory.
  39. // If you want to avoid a constructor on each state you can also do this via properties, either way setting it once on the factory propogates it to all created states
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement