java设计模式之备忘录模式求助
某系统提供了用户信息操作模块,用户可以修改自己的各项信息,现使用备忘录模式对系统进行改进,使用户可以实现多次撤销操作为什么我的结果不正确
结果是这样的:
恢复到状态一
账户刘潘
密码1234
电话32534635
账户刘潘
密码1234
电话32534635
账户刘潘
密码1234
电话32534635
请大神指教
我的代码如下
//原发器(用户信息类)
package 备忘录模式;
public class userInfo {
private String account;
private String password;
private String tel;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Memento saveMemento()
{
return new Memento(account, password, tel);
}
public void restoreMemento(Memento memento)
{
this.account=memento.getAccount();
this.password=memento.getPassword();
this.tel=memento.getTel();
}
public void show()
{
System.out.println("账户"+this.account+'\n'+"密码"+this.password+'\n'+"电话"+this.tel);
}
}
//备忘录类
package 备忘录模式;
import java.util.ArrayList;
public class Memento {
private String account;
private String password;
private String tel;
//ArrayList array=new ArrayList ();
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Memento(String account, String password, String tel) {
this.account = account;
this.password = password;
this.tel = tel;
//array.add(memento);
}
}
//负责人CareTaker类
package 备忘录模式;
import java.util.ArrayList;
public class CareTaker {
private Memento memento;
public static ArrayList array=new ArrayList ();
public Memento getMemento()
{
return memento;
}
public void setMemento(Memento mm)
{
this.memento=mm;
array.add(memento);
}
public ArrayList getArr()
{
return this.array;
}
}
//用户端测试类
package 备忘录模式;
import java.util.ArrayList;
public class client {
public static void main(String args[])
{
CareTaker cc=new CareTaker();
userInfo user=new userInfo();
user.setAccount("minmin");
user.setPassword("12345");
user.setTel("32534635");
cc.setMemento(user.saveMemento());
//user.show();
user.setAccount("刘敏");
user.setPassword("1234");
user.setTel("32534635");
cc.setMemento(user.saveMemento());
//user.show();
user.setAccount("刘潘");
user.setPassword("1234");
user.setTel("32534635");
cc.setMemento(user.saveMemento());
//user.show();
user.setAccount("敏");
user.setPassword("1234");
user.setTel("32534635");
//user.show();
//user.restoreMemento(cc.getMemento());
System.out.println("恢复到状态一");
//user.show();
ArrayList aa=cc.getArr();
for(int i=aa.size()-1;i>=0;i--)
{
user.restoreMemento(((Memento)cc.getArr().get(aa.size()-1)));
user.show();
}
}
}
--------------------编程问答--------------------
for (int i = aa.size() - 1; i >=0; i --)
{
user.restoreMemento((Memento)aa.get(i));
user.show();
}
楼主那样写,get出来的对象永远都是aa.size()-1个,也就是最后一个了,so...
补充:Java , Java SE