Advertisement
apieceoffruit

Untitled

May 5th, 2021
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1.  
  2.  
  3.  
  4. public class StateFactory
  5. {
  6.    GameElementData _data;
  7.    public StateFactory(GameElementData data) => _data = data;
  8.  
  9.    public State CreateStateA() => new StateA(_data);
  10.    public State CreateStateB() => new StateB(_data);
  11.    public State CreateStateC() => new StateC(_data);
  12.    public State CreateStateD() => new StateD(_data);
  13.  
  14. }
  15.  
  16. public abstract class State
  17. {
  18.    void Enter();
  19.    void Update();
  20. }
  21.  
  22.  
  23. class UsageExample : MonoBehaviour
  24. {
  25.    StateFactory _states;
  26.  
  27.    State _current;
  28.  
  29.    public void Awake()
  30.   {
  31.       _states = new StateFactory(new GameElemntDAta());
  32.  
  33.     _Current = _states.CreateStateA(); // etc
  34. }
  35.  
  36.  
  37. }
  38.  
  39. // now imagine adding an extra bit of data to all states, it has one place to do it, in the factory.
  40. // 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