Pattern Examples
Real-world patterns and complete examples to learn from.
Each pattern demonstrates a different aspect of EventMachine:
| Pattern | Demonstrates |
|---|---|
| Traffic Light | Custom context classes, typed behaviors, validation guards |
| Order Processing | Multi-step workflows, hierarchical states, result behaviors |
| Calculator | Event payloads, state machine operations, context updates |
| Elevator | Complex state logic, queued events, multi-path transitions |
| Guarded Transitions | Guard patterns, multi-branch transitions, conditional logic |
How to Use These Examples
- Start with Traffic Light - Simple example showing all core concepts
- Move to Order Processing - Real business workflow pattern
- Study Guarded Transitions - Understand conditional flow control
- Explore Calculator & Elevator - More complex state logic
Pattern Structure
Each pattern includes:
- Overview - What the pattern demonstrates
- State diagram - Visual representation
- Full code - Complete, runnable implementation
- Usage examples - How to use the machine
- Tests - How to test the pattern
- Key concepts - What you learned
Creating Your Own Patterns
Use these patterns as templates:
php
// 1. Define your context
class MyContext extends ContextManager { ... }
// 2. Define events, guards, actions
class MyEvent extends EventBehavior { ... }
class MyGuard extends GuardBehavior { ... }
class MyAction extends ActionBehavior { ... }
// 3. Build the machine
class MyMachine extends Machine
{
public static function definition(): MachineDefinition
{
return MachineDefinition::define(
config: [...],
behavior: [...],
);
}
}