State在 Ruby 中的实现
State is a behavioral design pattern that allows an object to change the behavior when its internal state changes.
The pattern extracts state-related behaviors into separate state classes and forces original object to delegate the work to an instance of these classes, instead of acting on its own.
在 Ruby 中使用模式
Usage examples: The State pattern is commonly used in Ruby to convert massive
switch-base state machines into the objects.
Identification: State pattern can be recognized by methods that change their behavior depending on the objects' state, controlled externally.
This example illustrates the structure of the State design pattern. It focuses on answering these questions:
- What classes does it consists of?
- What roles do these classes play?
- In what way the elements of the pattern are related?
main.rb: Conceptual Example
output.txt: Execution result