The before code mixes all event types in one method using string checks, causing confusion and tight coupling. The after code defines separate classes for each event type and processes them with dedicated handlers, improving clarity and separation of concerns.
### Before: Mixed event handling without clear types
class EventProcessor:
def process(self, event):
if event['type'] == 'order_created':
self.handle_order_created(event)
elif event['type'] == 'payment_received':
self.handle_payment(event)
elif event['type'] == 'send_email':
self.handle_notification(event)
### After: Clear event type separation
class DomainEvent:
def __init__(self, data):
self.data = data
class IntegrationEvent:
def __init__(self, data):
self.data = data
class NotificationEvent:
def __init__(self, data):
self.data = data
class EventProcessor:
def process(self, event):
if isinstance(event, DomainEvent):
self.handle_domain_event(event)
elif isinstance(event, IntegrationEvent):
self.handle_integration_event(event)
elif isinstance(event, NotificationEvent):
self.handle_notification_event(event)
def handle_domain_event(self, event):
# business logic
pass
def handle_integration_event(self, event):
# cross-service coordination
pass
def handle_notification_event(self, event):
# user or external notifications
pass