/ Design Patterns / Behavioral patterns / Command Command Design Pattern in Python Back to Command description """ Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. """ import abc class Invoker: """ Ask the command to carry out the request. """ def __init__(self): self._commands = [] def store_command(self, command): self._commands.append(command) def execute_commands(self): for command in self._commands: command.execute() class Command(metaclass=abc.ABCMeta): """ Declare an interface for executing an operation. """ def __init__(self, receiver): self._receiver = receiver @abc.abstractmethod def execute(self): pass class ConcreteCommand(Command): """ Define a binding between a Receiver object and an action. Implement Execute by invoking the corresponding operation(s) on Receiver. """ def execute(self): self._receiver.action() class Receiver: """ Know how to perform the operations associated with carrying out a request. Any class may serve as a Receiver. """ def action(self): pass def main(): receiver = Receiver() concrete_command = ConcreteCommand(receiver) invoker = Invoker() invoker.store_command(concrete_command) invoker.execute_commands() if __name__ == "__main__": main() Support our free website and own the eBook! 22 design patterns and 8 principles explained in depth 406 well-structured, easy to read, jargon-free pages 228 clear and helpful illustrations and diagrams An archive with code examples in 4 languages All devices supported: EPUB/MOBI/PDF formats Learn more...