1. 程式人生 > >python版本的命令模式

python版本的命令模式

self. name on() 執行 bst pre and main ini

# -*- 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()

python版本的命令模式