首页 > 编程语言 > 详细

python版本的命令模式

时间:2018-01-25 15:09:45      阅读:265      评论:0      收藏:0      [点我收藏+]
# -*- 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版本的命令模式

原文:https://www.cnblogs.com/gjinwei/p/8351116.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!