java使用命令设计模式,只需撤销
好吧,这是我第一次使用任何设计模式,所以请原谅我,但我真的很挣扎的概念。据我所知,这里需要一个命令接口,通常它会保存public void execute();在我的情况下,我想我只想要公共的void undo();我的问题是关于所有其他必修课的。我的程序基本上已经完成了,我试图在最后合并这个设计模式,以加入一个撤销功能。这是我用自己的方法定义的LinkedList。下面是一些代码:
public class LinkedList<E> implements Serializable{
/** number of Nodes on the list */
private int count;
/** the first Node in the list */
private Node head;
/** the last Node in the list */
private Node tail;
/**Stack holding commands*/
private Stack<Command> undo = new Stack<Command>();
/** Inserts a given student's data into the database
* @param student given student data to be inserted into database
* @return none*/
public void insert(Student student){
//case where input is null
if(student.getName() == null || student.getGNumber() == null)
throw new IllegalArgumentException();
Node temp = head;
//case where there is no list
if(head == null){
head = new Node(student, null);
tail = head;
count = 1;
}
//case where top has existing Nodes
else{
//brings reference to end of list
while(temp.getNext() != null)
temp = temp.getNext();
temp.setNext(new Node(student, null));
tail = temp;
count ++;
}
}
那会是接受者类吗?对于每个命令,我还需要一个单独的类吗?实际上,我想要的只是一个命令的撤销堆栈,每次调用一个方法时,我都会将它的撤销推到该堆栈上。对于所有的命令设计模式示例,我可以发现它们都是同一个光开关示例,它确实不能很好地显示一般需求
好吧,很抱歉说得太多了,我想这是TL;问题:
我可以在我的LinkedList中添加一个堆栈,然后在弹出堆栈的LinkedList中添加一个undo方法吗?在我的每个方法中添加一个push语句?还是我仍然需要完成命令设计模式的所有步骤和类
编辑: 经过一番思考,我意识到要将命令推入堆栈,我需要为每个方法提供命令对象,有没有办法在LinkedList类中实现这一点
共 (0) 个答案