I'm reading about the State pattern. I have only just begun, so of course I begin by reading the entire Wikipedia article on it.
I noticed that both of the examples in the article have some base abstract class or Java interface for a generic State's methods/functions. Then there are some states which inherit from the base and implement those methods/functions in different ways. Then there's a Context class which has a private member of type State and which, at any time, can be equal to an instance of one of the implementations. That context class also implements the same methods, and passes them onto the current state instance, and then has an additional method to change the state (or depending on design I understand the change of state could be a reaction to one of the implemented methods).
Why doesn't this context class specifically "extend" or "implement" the generic State base class/interface?
Because the state is an implementation detail, not part of its interface. I.e. the Context is not a State, it only has a State. Users of the Context need not even be aware of it having a state.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With