design-patterns/behavioral/command/commands.py
Ruidy c8cc1f47b8
Observer (#20)
* doc: create package & update general TOCs

* doc: add documentation

* add code example

* reformat using black
2020-10-06 08:25:18 +02:00

44 lines
1.2 KiB
Python

from behavioral.command.command import Command
from behavioral.command.receiver import Receiver
class SimpleCommand(Command):
"""
Some commands can implement simple operations on their own.
"""
def __init__(self, payload: str) -> None:
self._payload = payload
def execute(self) -> None:
print(
f"SimpleCommand: See, I can do simple things like printing ({self._payload})"
)
class ComplexCommand(Command):
"""
However, some commands can delegate more complex operations to other
objects, called "receivers."
"""
def __init__(self, receiver: Receiver, a: str, b: str) -> None:
"""
Complex commands can accept one or several receiver objects along with
any context data via the constructor.
"""
self._receiver = receiver
self._a = a
self._b = b
def execute(self) -> None:
"""
Commands can delegate to any methods of a receiver.
"""
print(
"ComplexCommand: Complex stuff should be done by a receiver object", end=""
)
self._receiver.do_something(self._a)
self._receiver.do_something_else(self._b)