11.20每日总结
今天早上进行了软件设计模式的实验16
实验16:命令模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解命令模式的动机,掌握该模式的结构;
2、能够利用命令模式解决实际问题。
[实验任务一]:多次撤销和重复的命令模式
某系统需要提供一个命令集合(注:可以使用链表,栈等集合对象实现),用于存储一系列命令对象,并通过该命令集合实现多次undo()和redo()操作,可以使用加法运算来模拟实现。
实验要求:
1. 提交类图;
2. 提交源代码;
3. 注意编程规范。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | package test; import java.util.Stack; public abstract class AbstractCommand { public abstract int execute( int value); public abstract int undo(); public abstract int redo(); } public class AddCommand extends AbstractCommand { private Adder adder = new Adder(); private Stack<Integer> unStack = new Stack<Integer>(); // 返回栈,用来记录所做的每一步操作,用于撤回 private Stack<Integer> reStack = new Stack<Integer>(); // 重复栈,用来存储返回栈弹出的数据,由于重复 /** * 撤回 * */ public int undo() { int i= 0 ; if (unStack.isEmpty()) { i=- 1 ; } else { Integer pop = unStack.pop(); reStack.push(pop); if (!unStack.isEmpty()){ //判断弹出数据后是否为空,如果为空,说明已撤回到最原始状态 i=unStack.peek(); } } return i; } /** * 恢复 */ public int redo() { int i= 0 ; if (reStack.isEmpty()) { i=- 1 ; } else { //撤回时只要可以可以撤回,则返回栈一定有数据 Integer pop = reStack.pop(); unStack.push(pop); i=pop; } return i; } /** * 执行计算,并进行栈的更新 */ public int execute( int value) { int v = 0 ; if (unStack.isEmpty()) { // 说明还没有数据 v = adder.add(value); unStack.push(v); } else { // 需要更新两个栈中的内容,并计算结果,其中返回栈应该更新,重复栈应该清空 v = adder.add(value); unStack.push(v); if (!reStack.isEmpty()) { for ( int i = 0 ; i < reStack.size(); i++) { reStack.pop(); } } } return v; } } public class Adder { private int num = 0 ; public int add( int value) { num+=value; return num; } } public class CalculatorForm { private AbstractCommand command; public void setCommand(AbstractCommand command) { this .command =command; } /** * 执行运算 * @param value */ public void compute( int value) { command.execute(value); } /** * 撤回 */ public void undo() { int i = command.undo(); if (i==- 1 ){ System.out.println( "缓存中已不存在数据" ); } else { System.out.println( "执行成功,运算结果是:" +i); } } /** * 恢复 */ public void redo() { int i = command.redo(); if (i==- 1 ){ System.out.println( "已恢复至最新数据" ); } else { System.out.println( "执行成功,运算结果是:" +i); } } } public class Client { public static void main(String[] args) { CalculatorForm form = new CalculatorForm(); AddCommand command = new AddCommand(); form.setCommand(command); //计算 System.out.println( "------计算过程------" ); form.compute( 1 ); form.compute( 2 ); form.compute( 3 ); form.compute( 4 ); //多次撤回 System.out.println( "------撤回过程------" ); form.undo(); form.undo(); form.undo(); form.undo(); form.undo(); //多次恢复 System.out.println( "------恢复过程------" ); form.redo(); form.redo(); form.redo(); form.redo(); form.redo(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix