pyqt_reactive.services.enum_dispatch_service
Abstract base class for enum-driven polymorphic dispatch services.
This ABC eliminates duplication across services that use the same pattern: 1. Define an enum for strategies/types 2. Create a dispatch table mapping enum values to handler methods 3. Determine which strategy to use based on input 4. Dispatch to the appropriate handler
Services using this pattern: - ParameterResetService (ResetStrategy enum) - NestedValueCollectionService (ValueCollectionStrategy enum) - Widget creation (WidgetCreationType enum)
Benefits: - Single source of truth for dispatch pattern - Enforces consistent structure across services - Reduces boilerplate in service implementations - Makes adding new services trivial
Example
- class MyStrategy(Enum):
TYPE_A = “type_a” TYPE_B = “type_b”
- class MyService(EnumDispatchService[MyStrategy]):
- def __init__(self):
super().__init__() self._register_handlers({
MyStrategy.TYPE_A: self._handle_type_a, MyStrategy.TYPE_B: self._handle_type_b,
})
- def _determine_strategy(self, context, **kwargs) -> MyStrategy:
# Logic to determine which strategy to use return MyStrategy.TYPE_A if some_condition else MyStrategy.TYPE_B
- def _handle_type_a(self, context, **kwargs):
# Handler implementation pass
- def _handle_type_b(self, context, **kwargs):
# Handler implementation pass
- def process(self, context, **kwargs):
# Public API - uses dispatch return self.dispatch(context, **kwargs)
Classes
Abstract base class for services using enum-driven polymorphic dispatch. |
- class pyqt_reactive.services.enum_dispatch_service.EnumDispatchService[source]
Abstract base class for services using enum-driven polymorphic dispatch.
Subclasses must: 1. Define a strategy enum (e.g., ResetStrategy, ValueCollectionStrategy) 2. Implement _determine_strategy() to select the appropriate strategy 3. Register handlers in __init__() using _register_handlers() 4. Implement handler methods for each strategy
The dispatch() method handles the actual dispatching logic.
- dispatch(*args, **kwargs) Any[source]
Dispatch to the appropriate handler based on determined strategy.
This is the core dispatch logic that: 1. Determines the strategy using _determine_strategy() 2. Looks up the handler in the registry 3. Calls the handler with the provided arguments
- Parameters:
*args – Positional arguments to pass to the handler
**kwargs – Keyword arguments to pass to the handler
- Returns:
Result from the handler method
- Raises:
KeyError – If strategy is not registered in handlers