State and strategy in JavaScript and a simplified approach
The state and strategy patterns are closely related, in that they allow the extension of a software system‘s functionality by changing decoupled implementation objects, instead of changing the core subject object.
State allows an object to display different behavior based on what state it’s in. This is very useful for modeling state machines. Each state provides the same interface, and the core object calls methods on the different states.
Strategy similarly allows an object to dynamically select an implementation at runtime. In order to do this, the implementation is injected into the object and used.
We can classify the state pattern as a subset of the strategy pattern, where the implementation is dynamically changed by the state instances.
Next, we’ll see how to implement a state machine in JavaScript with the state pattern, as well as implement an object, merging abstraction with the strategy...