1. 程式人生 > >行為模式-備忘錄模式

行為模式-備忘錄模式

  行為型模式又可以分成以下四類:第一類:通過父類與子類的關係進行實現。第二類:兩個類之間。第三類:類的狀態。第四類:通過中間類

  備忘錄模式屬於第三類,類的狀態

  記錄一個類的某個時刻的狀態資訊,在未來的某個時刻可以恢復原來的狀態

package behavior.pattern.status.memento;

import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.beanutils.PropertyUtils;

public class Originator {
	private int state1 = 0;  
    private String state2 = "";  
    private Date state3 = new Date();  
  
    
    public Memento createMemento() throws Exception{  
    	Map<String, Object> memento = PropertyUtils.describe(this);
    	memento.remove("class");
        return new Memento(memento); 
    }  
      
    public void restoreMemento(Memento memento) throws Exception{  
        if(memento != null && memento.getStateMap() != null && memento.getStateMap().size() > 0)
        {
        	for(Entry<String, Object> entry: memento.getStateMap().entrySet())
        	{
        		PropertyUtils.setProperty(this, entry.getKey(), entry.getValue());
        	}
        }
    }  
    public int getState1() {
		return state1;
	}

	public void setState1(int state1) {
		this.state1 = state1;
	}

	public String getState2() {
		return state2;
	}

	public void setState2(String state2) {
		this.state2 = state2;
	}

	public Date getState3() {
		return state3;
	}

	public void setState3(Date state3) {
		this.state3 = state3;
	}

	public String toString(){  
        return "state1="+state1+"state2="+state2+"state3="+state3;  
    } 
}
package behavior.pattern.status.memento;

import java.util.Map;

public class Memento {
	private Map<String, Object> stateMap;  
    
    public Memento(Map<String, Object> map){  
        this.stateMap = map;  
    }  
  
    public Map<String, Object> getStateMap() {  
        return stateMap;  
    }  
  
    public void setStateMap(Map<String, Object> stateMap) {  
        this.stateMap = stateMap;  
    }
}

 上面兩個類一個是要儲存狀態的類,一個是儲存狀態資訊的備忘錄類

package behavior.pattern.status.memento;

import java.util.HashMap;
import java.util.Map;

public class Caretaker {
	Map<String, Memento> memMap = new HashMap<String, Memento>();  
    public Memento getMemento(String index){  
        return memMap.get(index);  
    }  
      
    public void setMemento(String index, Memento memento){  
        this.memMap.put(index, memento);  
    }
}

   上面的類是管理狀態的類,包括記錄和恢復狀態

  測試程式碼

package behavior.pattern.status.memento;

import java.util.Date;

public class Client {
	public static void main(String[] args) throws Exception{  
        Originator ori = new Originator();  
        Caretaker caretaker = new Caretaker();  
        ori.setState1(111);  
        ori.setState2("強盛");  
        ori.setState3(new Date());  
        System.out.println("===初始化狀態===\n"+ori);  
          
        caretaker.setMemento("001",ori.createMemento());  
        ori.setState1(222);  
        ori.setState2("架構");  
        ori.setState3(new Date());  
        System.out.println("===修改後狀態===\n"+ori);  
          
        ori.restoreMemento(caretaker.getMemento("001"));  
        System.out.println("===恢復後狀態===\n"+ori);  
	}
}

   執行結果