UML图和命令模式
一.验证命令模式程序一,实现作战场景命令传达的模拟
1.实验内容和源程序
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
| CompanyArmy.java public class CompanyArmy{ public void sneakAttack(){ System.out.println("我们知道如何偷袭敌人,保证完成任务"); } } Command.java public interface Command { public abstract void execute(); } ConcreteCommand.java public class ConcreteCommand implements Command{ CompanyArmy army; ConcreteCommand(CompanyArmy army){ this.army=army; } public void execute(){ army.sneakAttack(); } } ArmySuperior.java public class ArmySuperior{ Command command; public void setCommand(Command command){ this.command=command; } public void startExecuteCommand(){ command.execute(); } } Application.java public class Application{ public static void main(String args[]){ CompanyArmy 三连=new CompanyArmy(); Command command=new ConcreteCommand(三连); ArmySuperior 指挥官=new ArmySuperior(); 指挥官.setCommand(command); 指挥官.startExecuteCommand(); } }
|
2.实验执行结果
3.本次收获体会
明白了接受者,命令接口,具体命令,请求者的关系,用过三者可以构成一个小框架,然后使用这个小框架中的类编写应用程序
二.验证命令模式程序二,实现目录建立与撤销的模拟。(第二个实验内容)
1. 实验内容和源程序
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
| MakeDir.java import java.io.*; public class MakeDir{ public void createDir(String name){ File dir=new File(name); dir.mkdir(); } public void deleteDir(String name){ File dir=new File(name); dir.delete(); } } Command.java public interface Command { public abstract void execute(String name); public abstract void undo(); } ConcreteCommand.java import java.util.*; public class ConcreteCommand implements Command{ ArrayList<String> dirNameList; MakeDir makeDir; ConcreteCommand(MakeDir makeDir){ dirNameList=new ArrayList<String>(); this.makeDir=makeDir; } public void execute(String name){ makeDir.createDir(name); dirNameList.add(name); } public void undo(){ if(dirNameList.size()>0){ int m=dirNameList.size(); String str=dirNameList.get(m-1); makeDir.deleteDir(str); dirNameList.remove(m-1); } else System.out.println("没有需要撤消的操作"); } } RequestMakedir.java public class RequestMakedir{ Command command; public void setCommand(Command command){ this.command=command; } public void startExecuteCommand(String name){ command.execute(name); } public void undoCommand(){ command.undo(); } } Application.java public class Application{ public static void main(String args[]){ MakeDir makeDir=new MakeDir(); Command command=new ConcreteCommand(makeDir); RequestMakedir askMakedir=new RequestMakedir(); askMakedir.setCommand(command); askMakedir.startExecuteCommand("jiafei"); askMakedir.startExecuteCommand("jerry"); askMakedir.startExecuteCommand("tom"); askMakedir.undoCommand(); askMakedir.undoCommand(); } }
|
2.实验执行结果
3.本次收获体会
明白了命令接口中的撤销方法,可以用来封装“请求”的办法,请求者请求在硬盘建立目录,请求成功后,还可以撤销请求。这要求接受者不仅可以在硬盘上建立目录,也可删除上一次请求所建立的目录。
三、下载安装Rational Rose,并用它画UML类图
本文使用 CC BY-NC-SA 3.0 中国大陆 协议许可
具体请参见 知识共享协议
本文链接:https://zyhang8.github.io/2019/11/06/design-exp1/