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 acti
How I'd go about this
class Command(object):
def execute(self, canvas):
raise NotImplementedError
class DrawLineCommand(Command):
def __init__(self, point1, point2):
self._point1 = point1
self._point2 = point2
def execute(self, canvas):
canvas.draw_line(self._point1, self._point2)
class DrawCircleCommand(Command):
def __init__(self, point, radius):
self._point = point
self._radius = radius
def execute(self, canvas):
canvas.draw_circle(self._point, self._radius)
class UndoHistory(object):
def __init__(self, canvas):
self._commands = []
self.canvas = canvas
def command(self, command):
self._commands.append(command)
command.execute(self.canvas)
def undo(self):
self._commands.pop() # throw away last command
self.canvas.clear()
for command self._commands:
command.execute(self.canvas)
Some thoughts: