Book Image

Mastering Python Design Patterns

By : Sakis Kasampalis
Book Image

Mastering Python Design Patterns

By: Sakis Kasampalis

Overview of this book

Table of Contents (23 chapters)
Mastering Python Design Patterns
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
The Factory Pattern
Index

Chapter 14. The State Pattern

Object-oriented programming focuses on mutating the state of objects that interact with each other. A very handy tool to model (and when necessary, mathematically formalize) state transitions in many problems is using a finite-state machine (commonly known as a state machine) First, what's a state machine? A state machine is an abstract machine that has two key components: states and transitions. A state is the current (active) status of a system. For example, if we have a radio receiver, two possible states are tuning on the FM or AM. Another possible state is switching from one FM/AM radio station to another. A transition is the switch from one state to another. A transition is initiated by a triggering event or condition. Usually, an action or set of actions is executed before or after a transition occurs. Assuming that our radio receiver is tuned on the 107 FM station, an example of a transition is the button pressed by the listener to switch to 107.5 FM...