This design pattern should be considered when the application needs to maintain certain state machine.
The State Pattern suggests that:
- Encapsulate each state into a separate class.
- The Context delegates to the current state to handle requests.
- Once a request is handled, the current state may change.
- Encapsulate what varies.
- Favor composition over inheritance.
- Open-Closed principle: closed for modification but open for extension.
State and Strategy patterns are similar in structure, but what they intent to do is quite different.
In case of Strategy Pattern, objects stick to one particular kind of a behavior.
In case of State Pattern, objects change state over time according to some well defined state transitions.
In case of Strategy Pattern, objects stick to one particular kind of a behavior.
In case of State Pattern, objects change state over time according to some well defined state transitions.
Vending machine implementation is a very good application of a State Pattern.
No comments:
Post a Comment