design-patterns/behavioral/chain_responsibility/abstract_handler.py
Ruidy 912226c9a9
Chain of Responsibility (#15)
* 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>
2020-09-30 14:12:08 +02:00

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