State template. Why doesn't the context class implement or inherit abstract abbreviations / class?

I read about the state template. I just started, so of course I start by reading the entire Wikipedia article about this.

I noticed that both examples in the article have a base abstract class or Java interface for common State methods / functions. Then there are some states that inherit from the base and implement these methods / functions in different ways. Then there is the Context class, which has a private member of the state type and which at any time can be equal to an instance of one of the implementations. This context class also implements the same methods and passes them to the current instance of the state, and then has an additional method of changing the state (or depending on the design, I understand that changing the state may be a reaction to one of the implemented methods).

Why does this context class not specifically “extend” or “implement” a common base class / database interface?

+3
source share
1 answer

Because state is a part of the implementation, not part of its interface. That is, the Context is not a state, it has only a state. Context users should not even be aware of a state.

+5
source

Source: https://habr.com/ru/post/1747592/


All Articles