The state pattern changes how an object behaves based on changes in its internal state. This object seems to ‘magically’ change.
More specifically, a state variable is held in a class called the ‘context’. The state inside the context is updated by concrete implementations of a state interface.
• Make Get Call
• Review output – In this sample, my context’s state moves from undefined to state 1, 2, 3, 4 and finally 5.
I chose to implement the pattern’s defined components (see reference #1) with the following modules:
• Context – Context.js has a reference to a state object and a method to return the current state.
• State Base -AbsStateBase.js which has the method execute that is implemented by each concrete State object. It takes the context as an argument.
• Concrete Implementations (created in stateDriver.js) ⁃ Each implementation sets the state of the context to itself. States go from undefined to state 5.
▪ Context is a class variable in stateDriver.js
▪ Context’s state is set directly as a variable instead of as a method.