您希望您的程式中具有復(fù)原機(jī)制,如果您直接在物件中建立復(fù)原機(jī)制,這會使得物件本身的職責(zé)加重,並且使得物件的重用性降低。
與其在物件內(nèi)建立復(fù)原機(jī)制,不如將復(fù)原機(jī)制從物件中脫離出來,這個(gè)時(shí)候您可以使用Memento模式來達(dá)成這個(gè)功能。
Memento模式在Originator中保留一個(gè)Memento成員,這個(gè)Memento可以包括Originator的成員資訊,在外部的話, Memento可以由一個(gè)Caretaker維護(hù),每對Originator作一個(gè)動作,Caretaker就保留Originator動作前的成員狀態(tài),如果以後要復(fù)原物件狀態(tài),只要從Caretaker取回Memento物件,對Originator進(jìn)行狀態(tài)復(fù)原。
Memento模式的 UML 類別結(jié)構(gòu)圖如下所示:
圖中的Caretaker是用來保留原發(fā)者所創(chuàng)造的備忘錄物件,以供日後復(fù)原時(shí)取回,state表示一個(gè)內(nèi)部狀態(tài),內(nèi)部狀態(tài)多時(shí),也可以將之組織為一個(gè)類別,Caretaker維護(hù)的Memento可以是多個(gè),用來實(shí)現(xiàn)Redo與Undo多次的功能。
下面提供一個(gè)簡單的實(shí)作,看看如何實(shí)現(xiàn)Memento模式:
public class Originator {
private String name;
private String phone;
public Originator(String name, String phone) {
this.name = name;
this.phone = phone;
}
// Some operations make state changed
public void someOperation() {
name = "noboby";
phone = "911-911";
}
// recover object's state
public void setMemento(Memento m) {
this.name = m.getName();
this.phone = m.getPhone();
}
public Memento createMemento() {
return new Memento(name, phone);
}
public void showInfo() {
System.out.println("Name: " + name +
"\nPhone: " + phone + "\n");
}
}
public class Memento {
private String name;
private String phone;
public Memento(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public void setName(String name) {
this.name = name;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
public class Caretaker {
private Memento memento;
public void setMemento(Memento memento) {
this.memento = memento;
}
public Memento getMemento() {
return memento;
}
}
\
public class Main {
public static void main(String[] args) {
Originator originator =
new Originator("Justin", "888-8888");
Caretaker caretaker = new Caretaker();
// save object's memento
caretaker.setMemento(originator.createMemento());
originator.showInfo();
// some operations make the object's state changed
originator.someOperation();
originator.showInfo();
// use memento to recover object's state
originator.setMemento(caretaker.getMemento());
originator.showInfo();
}
}
可以結(jié)合
Command 模式 來實(shí)作Redo/Undo的功能,將操作前後的物件狀態(tài)記錄下來,並記錄所使用的命令,當(dāng)要實(shí)現(xiàn)Undo/Redo時(shí),只要取回Memento物件以復(fù)原物件狀態(tài)即可。