備忘錄模式應該是設計模式中最簡單的一個了,比如開會中找人來做備忘錄,就是這個意思。
public class Cahier {
private String name;
private String content;
private String persons;
public void setName (String name) {
this.name = name;
}
public String getName () {
return this.name;
}
public void setContent (String content) {
this.content = content;
}
public String getContent () {
return this.content;
}
public void setPersons (String persons) {
this.persons = persons;
}
public String getPersons () {
return this.persons;
}
public Memento getMemento() {
return new Memento(this);
}
public void setMemento(Memento memento) {
this.name = memento.getName();
this.content = memento.getContent ();
this.persons = memento.getPersons ();
}
public class Memento {
private String name;
private String content;
private String persons;
public Memento (Cahier cahier) {
this.name = cahier.getName();
this.content = cahier.getContent ();
this.persons = cahier.getPersons ();
}
public void setName (String name) {
this.name = name;
}
public String getName () {
return this.name;
}
public void setContent (String content) {
this.content = content;
}
public String getContent () {
return this.content;
}
public void setPersons (String persons) {
this.persons = persons;
}
public String getPersons () {
return this.persons;
}
public class Client {
public static void main(String[] argv) {
Cahier cahier = new Cahier();
cahier.setName("公司銷售會議");
cahier.setContent("有關銷售價格的會議內容");
cahier.setPersons("總經理、銷售處長");
System.out.println("原來的內容" + cahier.getName() + " :" + cahier.getContent() + " :" + cahier.getPersons());
Memento memento = cahier.getMemento();
//進行其它代碼的處理
cahier.setName("公司辦公會議");
cahier.setContent("有關員工穩定的會議內容");
cahier.setPersons("董事長、總經理、人事副總");
System.out.println("修改後的內容" + cahier.getName() + " :" + cahier.getContent() + " :" + cahier.getPersons());
//恢複原來的代碼
cahier.setMemento(memento);
System.out.println("恢複到原來的內容" + cahier.getName() + " :" + cahier.getContent() + " :" + cahier.getPersons());
}
}