Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

State pattern: Why doesn't the context class implement or inherit the State abstract interface/class?

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?

like image 335
Ricket Avatar asked May 28 '10 17:05

Ricket


1 Answers

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.

like image 199
Péter Török Avatar answered Sep 22 '22 22:09

Péter Török