Команда - это паттерн, который инкапсулирует запрос в виде объекта, позволяя параметризировать клиентов с разными запросами, ставить запросы в очередь.
Стоит использовать когда нужно параметризировать объекты выполняемыми действиями, когда требуется поддержка отмены операций, для очередей задач и в транзакционных системах, где нужно выполнять серии операций.
Пример: `class Light: def on(self): print("Свет включен")
def off(self): print("Свет выключен")
class Command(ABC): @abstractmethod def execute(self): pass
class LightOnCommand(Command): def init(self, light): self.light = light
def execute(self): self.light.on()
class LightOffCommand(Command): def init(self, light): self.light = light
def execute(self): self.light.off()
#Пульт управления с одной кнопкойclass RemoteControl: def init(self): self.command = None
def set_command(self, command): self.command = command
def press_button(self): if self.command: self.command.execute()
#Использованиеlight = Light() remote = RemoteControl()
remote.set_command(LightOnCommand(light)) remote.press_button() # Свет включен
remote.set_command(LightOffCommand(light)) remote.press_button() # Свет выключен #weekofpatterns`