Editor

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 lesson

Create an EventEmitter class with:

  • on(event, callback) -- register a callback for an event
  • emit(event, *args) -- call all callbacks registered for that event, passing *args
  • Then:

    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

    All Python exercises · 50 practice problems · Advanced Python quiz