The before code loads configuration once at startup, so changes require restart. The after code subscribes to a configuration service that pushes updates. The service updates its config dynamically and changes behavior immediately without restart.
### Before: Static config loaded once at startup
class Service:
def __init__(self):
self.config = load_config()
def run(self):
if self.config['feature_enabled']:
print('Feature ON')
else:
print('Feature OFF')
### After: Dynamic config with update callback
import threading
import time
class ConfigService:
def __init__(self):
self.config = {'feature_enabled': False}
self.subscribers = []
def subscribe(self, callback):
self.subscribers.append(callback)
def update_config(self, new_config):
self.config = new_config
for cb in self.subscribers:
cb(self.config)
class Service:
def __init__(self, config_service):
self.config = {}
config_service.subscribe(self.on_config_update)
def on_config_update(self, new_config):
self.config = new_config
print(f'Config updated: {self.config}')
def run(self):
if self.config.get('feature_enabled'):
print('Feature ON')
else:
print('Feature OFF')
# Simulate config changes
config_service = ConfigService()
service = Service(config_service)
# Initial run
service.run()
# Update config dynamically
config_service.update_config({'feature_enabled': True})
# Run again with updated config
service.run()