python撤销键_在Python中使用命令模式执行/撤消操作

我已经读到使用命令模式是完成do / undo功能的最流行的方法之一.实际上,我已经看到可以堆叠一堆动作并反转它们以达到给定状态.但是,我不太确定如何在Python中完成此工作,而且我已阅读的大多数教程都涉猎概念,但未显示Python的实际实现.

有谁知道Python中的功能/撤消功能如何工作?

供参考,这是我的代码(天真,可能充满了错误):

# command

class DrawCommand:

def __init__(self, draw, point1, point2):

self.draw = draw

self.point1 = point1

self.point2 = point2

def execute_drawing(self):

self.draw.execute(self.point1, self.point2)

def execute_undrawing(self):

self.draw.unexecute(self.point1, self.point2)

# invoker

class InvokeDrawALine:

def command(self, command):

self.command = command

def click_to_draw(self):

self.command.execute_drawing()

def undo(self):

self.command.execute_undrawing()

# receiver

class DrawALine:

def execute(self, point1, point2):

print("Draw a line from {} to {}".format(point1, point2))

def unexecute(self, point1, point2):

print("Erase a line from {} to {}".format(point1, point2))

实例化如下:

invoke_draw = InvokeDrawALine()

draw_a_line = DrawALine()

draw_command = DrawCommand(draw_a_line, 1, 2)

invoke_draw.command(draw_command)

invoke_draw.click_to_draw()

invoke_draw.undo()

输出:

Draw a line from 1 to 2

Erase a line from 1 to 2

显然,该测试不允许撤消堆栈中的多个操作.也许我完全弄错了,所以我将不胜感激.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值