问题描述
我已经读到使用命令模式是完成执行/撤消功能的最流行的方法之一.事实上,我已经看到可以堆叠一堆动作并反转它们以达到给定状态.但是,我不太确定如何在 Python 中做到这一点,而且我阅读的大多数教程都涉足概念,但没有展示 Python 中的实际实现.
I have read that using command pattern is one of the most popular ways to accomplish do/undo functionality. In fact, I have seen that it's possible to stack a bunch of actions and reverse them in order to reach a given state. However, I'm not quite sure how that can be done in Python and most of the tutorials I have read, dabble into concepts but don't show an actual implementation in Python.
有谁知道 Python 中的执行/撤消功能是如何工作的?
Does anyone know how do/undo functionality work in Python?
作为参考,这是我的(幼稚且可能充满错误)代码:
For reference, this is my (naive and probably ridden with errors) code:
实例化如下:
输出:
显然,此测试不允许堆叠多个操作来撤消.也许我完全错了,所以我会很感激一些帮助.
Obviously, this test doesn't allow stack several actions to undo. Maybe I'm completely mistaken so I would appreciate some help.
推荐答案
这是一个将命令保存在列表中的实现.
Here is an implementation keeping the commands in a list.
这篇关于在 Python 中使用命令模式执行/撤消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!