<i id='NjWZz'><tr id='NjWZz'><dt id='NjWZz'><q id='NjWZz'><span id='NjWZz'><b id='NjWZz'><form id='NjWZz'><ins id='NjWZz'></ins><ul id='NjWZz'></ul><sub id='NjWZz'></sub></form><legend id='NjWZz'></legend><bdo id='NjWZz'><pre id='NjWZz'><center id='NjWZz'></center></pre></bdo></b><th id='NjWZz'></th></span></q></dt></tr></i><div id='NjWZz'><tfoot id='NjWZz'></tfoot><dl id='NjWZz'><fieldset id='NjWZz'></fieldset></dl></div>
    <legend id='NjWZz'><style id='NjWZz'><dir id='NjWZz'><q id='NjWZz'></q></dir></style></legend>
    • <bdo id='NjWZz'></bdo><ul id='NjWZz'></ul>
    <tfoot id='NjWZz'></tfoot>

    1. <small id='NjWZz'></small><noframes id='NjWZz'>

        在 Python 中使用命令模式执行/撤消

        Do/Undo using command pattern in Python(在 Python 中使用命令模式执行/撤消)

            • <bdo id='WexJ7'></bdo><ul id='WexJ7'></ul>
              • <i id='WexJ7'><tr id='WexJ7'><dt id='WexJ7'><q id='WexJ7'><span id='WexJ7'><b id='WexJ7'><form id='WexJ7'><ins id='WexJ7'></ins><ul id='WexJ7'></ul><sub id='WexJ7'></sub></form><legend id='WexJ7'></legend><bdo id='WexJ7'><pre id='WexJ7'><center id='WexJ7'></center></pre></bdo></b><th id='WexJ7'></th></span></q></dt></tr></i><div id='WexJ7'><tfoot id='WexJ7'></tfoot><dl id='WexJ7'><fieldset id='WexJ7'></fieldset></dl></div>

                  <legend id='WexJ7'><style id='WexJ7'><dir id='WexJ7'><q id='WexJ7'></q></dir></style></legend>
                  <tfoot id='WexJ7'></tfoot>
                    <tbody id='WexJ7'></tbody>
                1. <small id='WexJ7'></small><noframes id='WexJ7'>

                2. 本文介绍了在 Python 中使用命令模式执行/撤消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经读到使用命令模式是完成执行/撤消功能的最流行的方法之一.事实上,我已经看到可以堆叠一堆动作并反转它们以达到给定状态.但是,我不太确定如何在 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:

                  # 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
                  

                  显然,此测试不允许堆叠多个操作来撤消.也许我完全错了,所以我会很感激一些帮助.

                  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.

                  # 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)
                  # invoker
                  class InvokeDrawLines:
                      def __init__(self, data):
                          self.commandlist = data
                      def addcommand(self, command):
                          self.commandlist.append(command)
                      def draw(self):
                          for cmd in self.commandlist:
                              cmd.execute_drawing()
                      def undocommand(self, command):
                          self.commandlist.remove(command)
                  
                  # receiver
                  class DrawALine:
                      def execute(self, point1, point2):
                          print("Draw a line from" , point1, point2)
                  

                  这篇关于在 Python 中使用命令模式执行/撤消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                        <tbody id='K1vtN'></tbody>

                      <small id='K1vtN'></small><noframes id='K1vtN'>

                      • <bdo id='K1vtN'></bdo><ul id='K1vtN'></ul>

                        • <i id='K1vtN'><tr id='K1vtN'><dt id='K1vtN'><q id='K1vtN'><span id='K1vtN'><b id='K1vtN'><form id='K1vtN'><ins id='K1vtN'></ins><ul id='K1vtN'></ul><sub id='K1vtN'></sub></form><legend id='K1vtN'></legend><bdo id='K1vtN'><pre id='K1vtN'><center id='K1vtN'></center></pre></bdo></b><th id='K1vtN'></th></span></q></dt></tr></i><div id='K1vtN'><tfoot id='K1vtN'></tfoot><dl id='K1vtN'><fieldset id='K1vtN'></fieldset></dl></div>

                          <legend id='K1vtN'><style id='K1vtN'><dir id='K1vtN'><q id='K1vtN'></q></dir></style></legend>
                          <tfoot id='K1vtN'></tfoot>