热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

《GOF设计模式》—命令(COMMAND)—Delphi源码示例:支持取消和重做(多次取消1)

示例:多次取消1说明:      若要支持多级的取消和重做,就需要有一个已被执行命令的历史列表(historylist),该列表的最大长度决定了取消和重做的级数。历史列表存储

示例:多次取消1
说明:
       若要支持多级的取消和重做,就需要有一个已被执行命令的历史列表 (historylist),该列表的最大长度决定了取消和重做的级数。历史列表存储了已被执行的命令序列。向后遍历该列表并逆向执行 (reverse-executing)命令是取消它们的结果;向前遍历并执行命令是重执行它们。
       如果该命令的状态在执行时从不改变,则不需要拷贝,而仅需将一个对该命令的引用放入历史列表中。

界面:
 clip_image002
object Form2: TForm2
  Left = 192
  Top = 110
  Width = 354
  Height = 200
  Caption = 'Form2'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate= FormCreate
  OnDestroy= FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object btnCalc1: TButton
    Left = 8
    Top = 128
    Width = 75
    Height = 25
    Caption = '计算1'
    TabOrder = 0
    OnClick= btnCalc1Click
  end
  object Memo1: TMemo
    Left = 64
    Top = 32
    Width = 193
    Height = 81
    Lines.Strings = (
      'Memo1')
    TabOrder = 1
  end
  object btnCalc2: TButton
    Left = 96
    Top = 128
    Width = 75
    Height = 25
    Caption = '计算2'
    TabOrder = 2
    OnClick= btnCalc2Click
  end
  object btnCancel: TButton
    Left = 184
    Top = 128
    Width = 75
    Height = 25
    Caption = '取消'
    TabOrder = 3
    OnClick= btnCancelClick
  end
  object btnRedo: TButton
    Left = 264
    Top = 128
    Width = 75
    Height = 25
    Caption = '重做'
    TabOrder = 4
    OnClick= btnRedoClick
  end
end

代码:
 

unit uCommand4;

interface

uses
    SysUtils,Controls,StdCtrls,classes;

type
    TReceiverState = record
        Count: Integer;
    end;

    TReceiver = class
    private
        FState: TReceiverState;
        FMemo: TMemo;
        procedure ShowState;
    public
        constructor Create(AMemo: TMemo);
        //---
        procedure Action();
        procedure Unaction();
    end;

    TCommand = class
    public
        procedure Execute(); virtual; abstract;
        procedure Unexecute; virtual; abstract;
    end;

    TCOncreteCommand= class(TCommand)
    private
        FReceiver: TReceiver;
    public
        constructor Create(AReceiver: TReceiver);
    end;
    TConcreteCommand_1 = class(TConcreteCommand)
    public
        procedure Execute; override;
        procedure Unexecute; override;
    end;
    TConcreteCommand_2 = class(TConcreteCommand)
    public
        procedure Execute; override;
        procedure Unexecute; override;
    end;

    THstoryCommand = class(TCommand)
    private
        FCommands: TList;
        FCurrent: integer;
    public
        constructor Create;
        destructor Destroy; override;
        //---
        procedure Add(ACommand: TCommand);
        //---
        procedure Execute(); override;
        procedure Unexecute; override;
    end;

implementation

constructor TReceiver.Create(AMemo: TMemo);
begin
    FMemo := AMemo;
    FState.Count := 0;
end;

procedure TReceiver.Action;
begin
    with FState do
        Count := Count + 1;
    self.ShowState;
end;

procedure TReceiver.Unaction;
begin
    with FState do
        Count := Count - 1;
    self.ShowState;
end;

procedure TReceiver.ShowState;
begin
    FMemo.Text := IntToStr(FState.Count);
end;

constructor TConcreteCommand.Create(AReceiver: TReceiver);
begin
    inherited Create;
    //---
    FReceiver := AReceiver;
end;

procedure TConcreteCommand_1.Execute;
begin
    FReceiver.Action;
end;

procedure TConcreteCommand_1.Unexecute;
begin
    FReceiver.Unaction;
end;

procedure TConcreteCommand_2.Execute;
begin
    FReceiver.Action;
    FReceiver.Action;
end;

procedure TConcreteCommand_2.Unexecute;
begin
    FReceiver.Unaction;
    FReceiver.Unaction;
end;

constructor THstoryCommand.Create;
begin
    inherited Create;
    //---
    FCommands := TList.Create;
    FCurrent := 0;
end;

destructor THstoryCommand.Destroy;
begin
    FCommands.Free;
    //---
    inherited;
end;

procedure THstoryCommand.Add(ACommand: TCommand);
const
    CNT_MAXLEVEL = 4;
    //---
    procedure _ClearCommand;
    var
        i: integer;
    begin
        with FCommands do
        begin
            if Count > 0 then
            begin
                for i := FCurrent - 1 downto 0 do
                    Delete(i);
            end;
        end;
        //---
        FCurrent := 0;
    end;
    //---
    procedure _CheckLevel;
    begin
        with FCommands do
        begin
            if Count = CNT_MAXLEVEL then
                Delete(Count - 1);
        end;
    end;
    //---
    procedure _AddCommand;
    begin
        FCommands.Insert(FCurrent,ACommand);
    end;
begin
    _ClearCommand;
    _CheckLevel;
    _AddCommand;
end;

procedure THstoryCommand.Execute();
{向前遍历并执行命令是重执行}
var
    ACommand: TCommand;
begin
    if (FCommands.Count > 0) and (FCurrent > 0) then
    begin
        FCurrent := FCurrent - 1;
        //---
        ACommand := FCommands[FCurrent];
        ACommand.Execute;
    end;
end;

procedure THstoryCommand.Unexecute;
{向后遍历该列表并逆向执行命令是取消}
var
    ACommand: TCommand;
begin
    if (FCurrent     begin
        ACommand := FCommands[FCurrent];
        ACommand.Unexecute;
        //---
        FCurrent := FCurrent + 1;
    end;
end;

end.

unit Unit2;

interface

uses
    Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
    Dialogs,StdCtrls,uCommand4;

type
    TForm2 = class(TForm)
        btnCalc1: TButton;
        Memo1: TMemo;
        btnCalc2: TButton;
        btnCancel: TButton;
        btnRedo: TButton;
        procedure FormDestroy(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure btnCalc1Click(Sender: TObject);
        procedure btnCalc2Click(Sender: TObject);
        procedure btnCancelClick(Sender: TObject);
        procedure btnRedoClick(Sender: TObject);
    private
        FReceiver: TReceiver;
        FCommand1,FCommand2: TCommand;
        FHistoryCommand: THstoryCommand;
    public
    { Public declarations }
    end;

var
    Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
    FReceiver := TReceiver.Create(self.Memo1);
    FCommand1 := TConcreteCommand_1.Create(FReceiver);
    FCommand2 := TConcreteCommand_2.Create(FReceiver);
    FHistoryCommand := THstoryCommand.Create;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
    FReceiver.Free;
    FCommand1.Free;
    FCommand2.Free;
    FHistoryCommand.Free;
end;

procedure TForm2.btnCalc1Click(Sender: TObject);
begin
    FCommand1.Execute;
    FHistoryCommand.Add(FCommand1);
end;

procedure TForm2.btnCalc2Click(Sender: TObject);
begin
    FCommand2.Execute;
    FHistoryCommand.Add(FCommand2);
end;

procedure TForm2.btnCancelClick(Sender: TObject);
begin
    FHistoryCommand.Unexecute;
end;

procedure TForm2.btnRedoClick(Sender: TObject);
begin
    FHistoryCommand.Execute;
end;

end.


推荐阅读
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • JS实现一键分享功能
    本文介绍了如何使用JS实现一键分享功能,并提供了2019独角兽企业招聘Python工程师的标准。同时,给出了分享到QQ空间、新浪微博和人人网的链接。 ... [详细]
author-avatar
mobiledu2502860983
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有