mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 18:46:41 +00:00
* chore: create behavioral design patterns package * doc: add chain of responsibility documentation * doc: add behavioral package table of content * doc: edit general doc * add code example Co-authored-by: Ruidy <r.nemausat@empfohlen.de>
27 lines
747 B
Python
27 lines
747 B
Python
from abc import abstractmethod
|
|
from typing import Any, Optional
|
|
|
|
from behavioral.chain_responsibility.handler import Handler
|
|
|
|
|
|
class AbstractHandler(Handler):
|
|
"""
|
|
The default chaining behavior can be implemented inside a base handler
|
|
class.
|
|
"""
|
|
|
|
_next_handler: Handler = None
|
|
|
|
def set_next(self, handler: Handler) -> Handler:
|
|
self._next_handler = handler
|
|
# Returning a handler from here will let us link handlers in a
|
|
# convenient way like this:
|
|
# monkey.set_next(squirrel).set_next(dog)
|
|
return handler
|
|
|
|
@abstractmethod
|
|
def handle(self, request: Any) -> Optional[str]:
|
|
if self._next_handler:
|
|
return self._next_handler.handle(request)
|
|
|
|
return None
|