# -*- coding:UTF-8 -*- import abc class Command(metaclass=abc.ABCMeta): def __init__(self, receiver): self._receiver = receiver @abc.abstractmethod def execute(self): pass class ConcreteCommand(Command): def execute(self): self._receiver.action() class Invoker: def __init__(self): self.__command = None def set_command(self, command): self.__command = command def execute_command(self): self.__command.execute() class Receiver: def action(self): print("执行请求") if __name__=="__main__": r = Receiver() c = ConcreteCommand(r) i = Invoker() i.set_command(c) i.execute_command()