State Pattern - should a state know about its context?

Posted by Extrakun on Programmers See other posts from Programmers or by Extrakun
Published on 2013-06-19T07:25:51Z Indexed on 2013/06/24 22:31 UTC
Read the original article Hit count: 231

Filed under:

I am referring to the state pattern as described in this link.

In the example class diagram, a context has numerous states. However, it does not show how does a state communicates with a context (perhaps an input in a state has impact on a setting in the context).

The two examples on the page shows either passing the context to the state via a function, or storing a reference to the context. Are those advisable?

Are there other ways for a state to communicate with a context?


Update to an edit:

For instance, I am doing a remote control which can have several states. Say, the context has a setting for Volume, and I am in one of the states which allow me to tweak the volume. How should the state communicates with the context that the volume is being changed?

© Programmers or respective owner

Related posts about design-patterns