Build an Event System
Advanced · Write the code · from Python Design Patterns: Factory, Strategy, Observer, and More
From the lesson: Python Design Patterns: Factory, Strategy, Observer, and More
Design patterns are proven recipes for solving common software problems. Think of them like architectural blueprints -- you wouldn't redesign plumbing from scratch for every house. Patterns give your code a shared vocabulary so other developers instantly understand your intent.
Read the lessonCreate an EventEmitter class with:
on(event, callback) -- register a callback for an eventemit(event, *args) -- call all callbacks registered for that event, passing *argsThen:
1. Create an emitter instance
2. Register a callback for 'greet' that prints Hello, followed by the argument
3. Register another callback for 'greet' that prints Hi there, followed by the argument
4. Emit 'greet' with argument 'World'
# Create EventEmitter and test it
More Design Patterns exercises
- Build a Shape FactoryAdvanced
- Strategy Pattern with FunctionsAdvanced
- Predict Singleton BehaviorAdvanced
- Implement the Decorator PatternAdvanced
- Template Method PatternAdvanced
All Python exercises · 50 practice problems · Advanced Python quiz