基于typescript严格模式以实现undo和redo功能为目标的命令模式代码参考

下面是一个完整的、严格模式下的 TypeScript 实现,包含 CommandManager、Command 和 CompositeCommand 类,支持 undo/redo 功能。

完整实现代码

1. Command 接口和基类

// src/commands/ICommand.ts
export interface ICommand {execute(): void;undo(): void;redo(): void;
}
// src/commands/Command.ts
import type{ ICommand } from "./ICommand";export abstract class Command implements ICommand {private _isExecuted: boolean = false;public execute(): void {if (!this._isExecuted) {this.doExecute();this._isExecuted = true;}}public undo(): void {if (this._isExecuted) {this.doUndo();this._isExecuted = false;}}public redo(): void {if (!this._isExecuted) {this.doExecute();this._isExecuted = true;}}protected abstract doExecute(): void;protected abstract doUndo(): void;public get isExecuted(): boolean {return this._isExecuted;}
}

2. CompositeCommand 实现

// src/commands/CompositeCommand.ts
import type{ ICommand } from "./ICommand";
import { Command } from "./Command";export class CompositeCommand extends Command {private readonly _commands: ICommand[] = [];public addCommand(command: ICommand): void {this._commands.push(command);}public removeCommand(command: ICommand): void {const index = this._commands.indexOf(command);if (index !== -1) {this._commands.splice(index, 1);}}public clearCommands(): void {this._commands.length = 0;}public get commands(): ReadonlyArray<ICommand> {return this._commands;}protected doExecute(): void {for (const command of this._commands) {command.execute();}}protected doUndo(): void {// Undo in reverse orderfor (let i = this._commands.length - 1; i >= 0; i--) {this._commands[i].undo();}}
}

3. CommandManager 实现

// src/commands/CommandManager.ts
import type{ ICommand } from "./ICommand";export class CommandManager {private readonly _undoStack: ICommand[] = [];private readonly _redoStack: ICommand[] = [];private readonly _maxStackSize: number;constructor(maxStackSize: number = 100) {this._maxStackSize = maxStackSize;}public execute(command: ICommand): void {command.execute();this._undoStack.push(command);// Clear redo stack when executing a new commandthis._redoStack.length = 0;// Ensure we don't exceed max stack sizeif (this._undoStack.length > this._maxStackSize) {this._undoStack.shift();}}public undo(): boolean {const command = this._undoStack.pop();if (command) {command.undo();this._redoStack.push(command);return true;}return false;}public redo(): boolean {const command = this._redoStack.pop();if (command) {command.redo();this._undoStack.push(command);return true;}return false;}public clearHistory(): void {this._undoStack.length = 0;this._redoStack.length = 0;}public get canUndo(): boolean {return this._undoStack.length > 0;}public get canRedo(): boolean {return this._redoStack.length > 0;}public get undoStackSize(): number {return this._undoStack.length;}public get redoStackSize(): number {return this._redoStack.length;}
}

示例使用代码

// src/main.ts
import { CommandManager } from "./commands/CommandManager";
import { Command } from "./commands/Command";
import { CompositeCommand } from "./commands/CompositeCommand";// 示例命令 - 修改文本
class ChangeTextCommand extends Command {constructor(private _target: { text: string },private _newText: string,private _oldText: string) {super();}protected doExecute(): void {this._target.text = this._newText;console.log(`Text changed to: ${this._newText}`);}protected doUndo(): void {this._target.text = this._oldText;console.log(`Text reverted to: ${this._oldText}`);}
}// 示例命令 - 修改数字
class ChangeNumberCommand extends Command {constructor(private _target: { value: number },private _newValue: number,private _oldValue: number) {super();}protected doExecute(): void {this._target.value = this._newValue;console.log(`Number changed to: ${this._newValue}`);}protected doUndo(): void {this._target.value = this._oldValue;console.log(`Number reverted to: ${this._oldValue}`);}
}// 使用示例
const commandManager = new CommandManager();const textObject = { text: "Initial text" };
const numberObject = { value: 0 };// 创建并执行单个命令
const changeTextCommand = new ChangeTextCommand(textObject, "New text", textObject.text);
commandManager.execute(changeTextCommand);// 创建并执行组合命令
const compositeCommand = new CompositeCommand();
compositeCommand.addCommand(new ChangeTextCommand(textObject, "Composite text", textObject.text));
compositeCommand.addCommand(new ChangeNumberCommand(numberObject, 42, numberObject.value));
commandManager.execute(compositeCommand);// 测试 undo/redo
console.log("--- Undo ---");
commandManager.undo(); // 撤销组合命令
console.log("Current text:", textObject.text);
console.log("Current number:", numberObject.value);console.log("--- Redo ---");
commandManager.redo(); // 重做组合命令
console.log("Current text:", textObject.text);
console.log("Current number:", numberObject.value);console.log("--- Undo single command ---");
commandManager.undo(); // 撤销组合命令
commandManager.undo(); // 撤销第一个文本修改命令
console.log("Current text:", textObject.text);

        在以上示例代码中,ChangeTextCommand类和ChangeNumberCommand类继承了 Command类,这两个类需要:

  1. 持有操作的目标对象和必要参数(通常通过构造函数注入)

  2. 实现具体的执行/撤销逻辑(doExecute()/doUndo()

而基类 Command 则负责:

  • 管理命令的执行状态

  • 提供执行流程的骨架

严格模式配置

确保你的 tsconfig.json 包含严格模式设置:

{"compilerOptions": {"strict": true,"noImplicitAny": true,"strictNullChecks": true,"strictFunctionTypes": true,"strictBindCallApply": true,"strictPropertyInitialization": true,"noImplicitThis": true,"alwaysStrict": true}
}

设计说明

  • ICommand 接口:定义了命令模式的基本操作。 

      这里需要说明一下,为什么先要定义一个ICommand接口,而不是直接从Command抽象类开始?主要是基于一下三点考虑:

  1. 允许未来添加不基于 Command 基类的其他命令实现

  2. 使 CommandManager 只依赖接口而不依赖具体实现(符合DIP原则)

  3. 为组合模式(如 CompositeCommand)提供了更清晰的类型约束

  • Command 抽象类

        基类 Command 额外提供了:执行状态跟踪_isExecuted防重复执行(通过检查状态),这使得子类可以更专注于业务逻辑,而不用重复编写状态管理代码。

  1. 实现了基础执行状态跟踪

  2. 要求子类实现实际执行和撤销逻辑

  • CompositeCommand

    • 可以组合多个命令一起执行

    • 撤销时按相反顺序执行

  • CommandManager

    • 管理 undo/redo 堆栈

    • 限制最大堆栈大小防止内存问题

    • 提供清晰的API和状态查询

这个实现是完全类型安全的,符合 TypeScript 严格模式要求,并且可以直接集成到 Vite 项目中。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/91790.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/91790.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2022年CIE SCI2区TOP,NSGA-II+直升机-无人机搜救任务分配,深度解析+性能实测

目录1.摘要2.数学模型3.求解方法4.结果展示5.参考文献6.代码获取7.算法辅导应用定制读者交流1.摘要 无人机任务分配对于保障搜救活动高效有序开展具有重要意义&#xff0c;但现有研究较少考虑无人机作业环境与性能对任务分配的影响。针对低空风场和地形因素对无人机能耗与性能…

暑期算法训练.4

目录 15.力扣 904.水果成篮 15.1 题目解析&#xff1a; 15.2 算法思路&#xff1a; 15.2.1 暴力解法&#xff1a; 15.2.1 滑动窗口 15.3代码演示&#xff1a; 15.4 总结反思&#xff1a; 16 力扣 438.找出字符串中所有字母的异位词 16.1 题目解析&#xff1a; 16.2算法…

关于个人博客系统的测试报告

1&#xff09;项目背景2&#xff09;项目功能介绍 登陆写博客/编辑已存在博客删除博客注销 2&#xff09;基于项目功能设计相关测试用例3&#xff09;基于测试用例编写自动化测试 准备工作登陆界面相关博客首页相关博客详情页相关编辑博客相关删除博客相关注销相关 4&#xff0…

Spring Boot 与微服务详细总结

一、Spring Boot 核心概述 Spring Boot 是简化 Spring 应用开发的框架&#xff0c;作为 Spring 技术栈的整合方案和 J2EE 开发的一站式解决方案&#xff0c;其核心优势体现在&#xff1a; 快速创建独立运行的 Spring 项目&#xff0c;轻松集成主流框架内置 Servlet 容器&…

轻松上手:从零开始启动第一个 Solana 测试节点

嗨&#xff0c;各位技术爱好者们&#xff01; 大家是否对 Solana 的“光速”交易处理能力感到好奇&#xff1f;或者你是一名开发者&#xff0c;正准备在 Solana 上构建下一个杀手级 dApp&#xff1f;无论大家是出于学习目的还是实际开发需求&#xff0c;亲手运行一个 Solana 节…

Gerrit workflow

提交代码 每次提交代码前&#xff0c;先执行 git pull --rebase &#xff0c;确保已经合并天上代码&#xff0c;解决冲突 git add git commit -m git push origin HEAD:refs/for/{BRANCH_NAME} 可考虑设置 alias 方式&#xff0c;参考下文 CR-2 情况处理(verify-1情况一样处理…

量化交易如何查询CFD指数实时行情

CFD即所谓的差价合约&#xff0c;是投资者在不拥有实际资产的情况下&#xff0c;交易金融市场的一种方式。最近笔者研究这一块比较多&#xff0c;但查遍整个中文互联网却很少找到关于CFD实时行情的查询教程。因此有了这篇文章。以下我将通过一个简单的Python代码示例&#xff0…

sql练习二

首先&#xff0c;建表。创建学生表和score表接着导入创建好基础信息就可以开始做了。3、分别查询student表和score表的所有记录4、查询student表的第2条到第5条记录5、从student表中查询计算机系和英语系的学生的信息6、从student表中查询年龄小于22岁的学生信息7、从student表…

windows11下基于docker单机部署ceph集群

windows下基于docker单机部署ceph集群 创建ceph专用网络 docker network create --driver bridge --subnet 172.20.0.0/16 ceph-network查看是否创建成功&#xff08;查看创建状态&#xff09; docker network inspect ceph-network拉取镜像&#xff1a;(镜像源自行选择) docke…

使用DataGrip连接安装在Linux上的Redis

目录 一、前言 二、开放防火墙端口 三、使用DataGrip连接安装在Linux上的Redis 一、前言 在学习黑马Redis从入门到实战的视频&#xff0c;完成了Redis在linux上的安装配置之后&#xff0c;我们可以使用图形化界面方便操作使用redis数据库。在24年JavaWebAI学习时连接MySQL数…

MySQL的union、union all导致排序失效

今天练习SQL&#xff0c;使用union all 连接各个查询导致我的各个查询排序失效&#xff0c;最后发现使用union all后会忽略各个模块的order by&#xff0c;只有最外层的order by才会生效原SQL如下&#xff1a;( selectexam_id tid,count(distinct uid) uv, count(uid) pv frome…

LVS 集群技术实践:NAT 与 DR 模式的配置与对比

1 实验环境规划 实验目标是搭建一个负载均衡集群&#xff0c;通过 LVS 调度器将流量分发到两台真实服务器&#xff08;RS1 和 RS2&#xff09;。2.网络配置3 实验步骤关闭防火墙和 SELinux安装 HTTP 服务&#xff08;在 RS21和 RS2 上&#xff09;&#xff1a;sudo systemctl s…

YOLOv8中添加SENet注意力机制

注意力机制(Attention Mechanism)是深度学习中的一种方法,在图像处理领域,尤其是在卷积神经网络(CNN)和视觉Transformer等架构中。图像数据具有局部相关性,注意力机制可以帮助模型聚焦于图像中更重要的区域,从而提升处理效果。 SENet(Squeeze-and-Excitation Network)…

SpringBoot五分钟快速入门指南

使用 Spring Boot 构建应用 本指南提供了关于Spring Boot如何帮助您加速应用开发的一些示例。随着您阅读更多 Spring 入门指南,您将看到 Spring Boot 的更多用例。本指南旨在让您快速了解 Spring Boot。如果您想创建自己的基于 Spring Boot 的项目,请访问 Spring Initializr…

docker,防火墙关闭后,未重启docker,导致端口映射失败

首先&#xff0c;看这篇文章前&#xff0c;建议先把网上其他的文章说的方法尝试一遍&#xff01;&#xff01;&#xff01; 1. 现象 docker启动某一个容器&#xff0c;然后映射端口时显示失败2. 解决 把网上的方法尝试一遍之后&#xff0c;最后发现是防火墙的问题&#xff01;&…

事务处理与AOP(web后端笔记第四期)

p.s.这是萌新自己自学总结的笔记&#xff0c;如果想学习得更透彻的话还是请去看大佬的讲解 目录事务spring事物管理事物属性--回滚事物属性--传播行为(propagation)AOP一些核心概念通知类型通知的执行顺序切入点表达式executionannotation连接点事务 事物是一组操作的集合&…

第36周———— RNN实现阿尔茨海默病诊断

目录 前言 1.检查GPU 2.查看数据 3.划分数据集 4.创建模型与编译训练 ​​​​5.编译及训练模型 6.结果可视化 7.模型预测 8.总结&#xff1a; 前言 &#x1f368; 本文为&#x1f517;365天深度学习训练营中的学习记录博客 &#x1f356; 原作者&#xff1a;K同学啊 1.检查G…

equals和hashcode方法重写

在 Java 中&#xff0c;当你需要基于对象的内容而非引用地址来判断两个对象是否相等时&#xff0c;就需要重写equals和hashCode方法。以下是具体场景和实现原则&#xff1a;一、为什么需要同时重写这两个方法&#xff1f;equals方法&#xff1a;默认比较对象的内存地址&#xf…

Excel批量生成SQL语句 Excel批量生成SQL脚本 Excel拼接sql

Excel批量生成SQL语句 Excel批量生成SQL脚本 Excel拼接sql一、情境描述在Excel中有标准的格式化数据&#xff0c;如何快速导入到数据库中呢&#xff1f;有些工具支持Excel导入的&#xff0c;则可以快速导入数据---例如Navicat&#xff1b;如果不支持呢&#xff0c;如果将Excel表…

金和OA C6 DelTemp.aspx 存在XML实体注入漏洞(CVE-2025-7523)

免责声明 本文档所述漏洞详情及复现方法仅限用于合法授权的安全研究和学术教育用途。任何个人或组织不得利用本文内容从事未经许可的渗透测试、网络攻击或其他违法行为。 前言:我们建立了一个更多,更全的知识库。每日追踪最新的安全漏洞,追中25HW情报。 更多详情: http…