java常用设计模式之备忘录模式
程序员文章站
2022-05-03 21:42:51
...
------------------------------ 备忘录模式(Memento) -------------------------------
(1)
备忘录模式属于行为型模式,其意图是在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这
个状态,这样以后就可以将对象恢复到原先保存的状态。
(2)
实例如下:
有一个对象Employee.除了属性外,还需要一个保存,还原状态的方法.
有一个对象Memento,用来记录Employee每一个时刻的状态,
CareTaker,用来保存,拿回Memento.需要一个保存,还原状态的方法.->需要一个指针,一个容器.
实例如下:
有一个对象Employee.除了属性外,还需要一个保存,还原状态的方法.
有一个对象Memento,用来记录Employee每一个时刻的状态,
CareTaker,用来保存,拿回Memento.需要一个保存,还原状态的方法.->需要一个指针,一个容器.
package memento;
public class Memento{
String name;
int age;
public Memento(String name,int age){
this.name = name;
this.age = age;
}
}
Employee模式:
package memento;
public class Employee{
private String name;
private int age;
public Employee(String aName,int aAge){
name = aName;
age = aAge;
}
public void setName(String aName){
name = aName;
}
public void setAge(int aAge){
age = aAge;
}
public Memento saveMemento(){
return new Memento(name,age);
}
public void restoreMemento(Memento memento){
age = memento.age;
name = memento.name;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
}
CareTaker代码:
package memento;
import java.util.Vector;
public class CareTaker{
private Vector v;
private int current;
public CareTaker(){
current = -1;
v = new Vector();
}
public void setMemento(Memento mem){
current ++;
v.add(mem);
}
public Memento getMemento(){
if(current>0){
current --;
return(Memento) v.get(current);
}
return null;
}
}
Client代码:
package memento;
public class Client{
public static void show(Employee e){
System.out.println("-----------------------------------");
System.out.println("Name:"+e.getName());
System.out.println("Age:" + e.getAge());
System.out.println("-----------------------------------");
}
public static void main(String[] args){
Employee e = new Employee("lili",25);
CareTaker ct = new CareTaker();
show(e);
ct.setMemento(e.saveMemento());
e.setName("litianli");
show(e);
ct.setMemento(e.saveMemento());
e.setAge(45);
show(e);
ct.setMemento(e.saveMemento());
//restore
e.restoreMemento(ct.getMemento());
show(e);
e.restoreMemento(ct.getMemento());
show(e);
}
}
public class Memento{
String name;
int age;
public Memento(String name,int age){
this.name = name;
this.age = age;
}
}
Employee模式:
package memento;
public class Employee{
private String name;
private int age;
public Employee(String aName,int aAge){
name = aName;
age = aAge;
}
public void setName(String aName){
name = aName;
}
public void setAge(int aAge){
age = aAge;
}
public Memento saveMemento(){
return new Memento(name,age);
}
public void restoreMemento(Memento memento){
age = memento.age;
name = memento.name;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
}
CareTaker代码:
package memento;
import java.util.Vector;
public class CareTaker{
private Vector v;
private int current;
public CareTaker(){
current = -1;
v = new Vector();
}
public void setMemento(Memento mem){
current ++;
v.add(mem);
}
public Memento getMemento(){
if(current>0){
current --;
return(Memento) v.get(current);
}
return null;
}
}
Client代码:
package memento;
public class Client{
public static void show(Employee e){
System.out.println("-----------------------------------");
System.out.println("Name:"+e.getName());
System.out.println("Age:" + e.getAge());
System.out.println("-----------------------------------");
}
public static void main(String[] args){
Employee e = new Employee("lili",25);
CareTaker ct = new CareTaker();
show(e);
ct.setMemento(e.saveMemento());
e.setName("litianli");
show(e);
ct.setMemento(e.saveMemento());
e.setAge(45);
show(e);
ct.setMemento(e.saveMemento());
//restore
e.restoreMemento(ct.getMemento());
show(e);
e.restoreMemento(ct.getMemento());
show(e);
}
}
上一篇: 16.设计模式之状态模式