Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StateFactory
- {
- GameElementData _data;
- public StateFactory(GameElementData data) => _data = data;
- public State CreateStateA() => new StateA(_data);
- public State CreateStateB() => new StateB(_data);
- public State CreateStateC() => new StateC(_data);
- public State CreateStateD() => new StateD(_data);
- }
- public abstract class State
- {
- void Enter();
- void Update();
- }
- class UsageExample : MonoBehaviour
- {
- StateFactory _states;
- State _current;
- public void Awake()
- {
- _states = new StateFactory(new GameElemntDAta());
- _Current = _states.CreateStateA(); // etc
- }
- }
- // now imagine adding an extra bit of data to all states, it has one place to do it, in the factory.
- // 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