design-patterns/behavioral/chain_responsibility/main.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

35 lines
1 KiB
Python

from behavioral.chain_responsibility.concrete_handlers import MonkeyHandler, SquirrelHandler, DogHandler
from behavioral.chain_responsibility.handler import Handler
def client_code(handler: Handler):
"""
The client code is usually suited to work with a single handler. In most
cases, it is not even aware that the handler is part of a chain.
"""
for food in ["Nut", "Banana", "Cup of coffee"]:
print(f"\nClient: Who wants a {food}?")
result = handler.handle(food)
if result:
print(f" {result}", end="")
else:
print(f" {food} was left untouched.", end="")
if __name__ == '__main__':
monkey = MonkeyHandler()
squirrel = SquirrelHandler()
dog = DogHandler()
monkey.set_next(squirrel).set_next(dog)
# The client should be able to send a request to any handler, not just the
# first one in the chain.
print("Chain: Monkey > Squirrel > Dog")
client_code(monkey)
print("\n")
print("Subchain: Squirrel > Dog")
client_code(squirrel)