[TOC]
一筋蓖、模式定義
備忘錄模式(Memento Pattern):備忘錄模式的定義是在不破壞封裝的前提下卸耘,捕獲一個(gè)對(duì)象的內(nèi)部狀態(tài)退敦,并將該對(duì)象之外保存這個(gè)狀態(tài)粘咖,這樣可以在以后將對(duì)象恢復(fù)到原先保存的狀態(tài)。所以備忘錄模式就是一種對(duì)象行為型模式侈百。
二瓮下、模式角色
備忘錄模式包括下面角色
- Originator(原發(fā)器)
- Memento(備忘錄)
- Caretaker(負(fù)責(zé)人)
備忘錄模式包括原發(fā)器類翰铡,備忘錄類和負(fù)責(zé)人類。原發(fā)器可以創(chuàng)建一個(gè)備忘錄讽坏,備忘錄類存儲(chǔ)原發(fā)器類的內(nèi)部狀態(tài)锭魔,根據(jù)原發(fā)器來決定保存哪些內(nèi)部狀態(tài),負(fù)責(zé)人類負(fù)責(zé)保存?zhèn)渫?/p>
三路呜、模式分析
備忘錄模式主要應(yīng)用于備份或者回退操作迷捧,為了使軟件使用更友好,通常都有回退功能胀葱,軟件一般也要提供回退機(jī)制漠秋,而要實(shí)現(xiàn)回退,就必須事先備份好狀態(tài)信息抵屿,所以有了備忘錄模式就有實(shí)現(xiàn)系統(tǒng)回退到某一個(gè)特定的歷史狀態(tài)庆锦。
備忘錄對(duì)象用于存儲(chǔ)另外一個(gè)對(duì)象內(nèi)部狀態(tài)的快照對(duì)象,所以備忘錄模式又可以稱之為快照模式(Snapshot Pattern)或Token模式
典型代碼:
原發(fā)器類:
public class Originator {
private String state;
public Originator(){}
// 創(chuàng)建一個(gè)備忘錄對(duì)象
public Memento createMemento(){
return new Memento(this);
}
// 根據(jù)備忘錄對(duì)象恢復(fù)原發(fā)器狀態(tài)
public void restoreMemento(Memento m){
state = m.state;
}
public void setState(String state)
{
this.state=state;
}
public String getState()
{
return this.state;
}
}
備忘錄類:
public class Memento {
private String state;
public Memento(Originator o){
state = o.state;
}
public void setState(String state)
{
this.state=state;
}
public String getState()
{
return this.state;
}
}
負(fù)責(zé)人類:
import java.util.ArrayList;
import java.util.List;
public class CareTaker {
private List<Memento> mementoList = new ArrayList<Memento>();
public void add(Memento state){
mementoList.add(state);
}
public Memento get(int index){
return mementoList.get(index);
}
}
四轧葛、模式例子
實(shí)例:用戶信息操作撤銷
某系統(tǒng)提供了用戶信息操作模塊搂抒,用戶可以修改自己的各項(xiàng)信息。為了使操作過程更加人性化尿扯,現(xiàn)使用備忘錄模式對(duì)系統(tǒng)進(jìn)行改進(jìn)求晶,使得用戶在進(jìn)行了錯(cuò)誤操作之后可以恢復(fù)到操作之前的狀態(tài)。
本例子來自《設(shè)計(jì)模式》一書
原發(fā)器類姜胖,創(chuàng)建備忘錄類
package dp.memento;
public class UserInfoDTO
{
private String account;
private String password;
private String telNo;
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 getTelNo()
{
return telNo;
}
public void setTelNo(String telNo)
{
this.telNo=telNo;
}
public Memento saveMemento()
{
return new Memento(account,password,telNo);
}
public void restoreMemento(Memento memento)
{
this.account=memento.getAccount();
this.password=memento.getPassword();
this.telNo=memento.getTelNo();
}
public void show()
{
System.out.println("Account:" + this.account);
System.out.println("Password:" + this.password);
System.out.println("TelNo:" + this.telNo);
}
}
備忘錄類誉帅,保存原發(fā)器類狀態(tài):
package dp.memento;
class Memento
{
private String account;
private String password;
private String telNo;
public Memento(String account,String password,String telNo)
{
this.account=account;
this.password=password;
this.telNo=telNo;
}
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 getTelNo()
{
return telNo;
}
public void setTelNo(String telNo)
{
this.telNo=telNo;
}
}
負(fù)責(zé)人類,創(chuàng)建備忘錄:
package dp.memento;
public class Caretaker
{
private Memento memento;
public Memento getMemento()
{
return memento;
}
public void setMemento(Memento memento)
{
this.memento=memento;
}
}
五右莱、模式應(yīng)用
- 軟件里的存檔操作
- Windows 里的 ctri + z蚜锨。
- IE 中的后退操作
- 數(shù)據(jù)庫的事務(wù)管理
....