设计模式之备忘录模式(行为型)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014427391/article/details/88598079

一、模式定义

备忘录模式(Memento Pattern):备忘录模式的定义是在不破坏封装的前提下,捕获一个对象的内部状态,并将该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态。所以备忘录模式就是一种对象行为型模式。

二、模式角色

备忘录模式包括下面角色

  • Originator(原发器)
  • Memento(备忘录)
  • Caretaker(负责人)

在这里插入图片描述

备忘录模式包括原发器类,备忘录类和负责人类。原发器可以创建一个备忘录,备忘录类存储原发器类的内部状态,根据原发器来决定保存哪些内部状态,负责人类负责保存备忘录

三、模式分析

备忘录模式主要应用于备份或者回退操作,为了使软件使用更友好,通常都有回退功能,软件一般也要提供回退机制,而要实现回退,就必须事先备份好状态信息,所以有了备忘录模式就有实现系统回退到某一个特定的历史状态。

备忘录对象用于存储另外一个对象内部状态的快照对象,所以备忘录模式又可以称之为快照模式(Snapshot Pattern)或Token模式

典型代码:

原发器类:

public class Originator {
  private String state;
  public Originator(){}
  // 创建一个备忘录对象
  public Memento createMemento(){
    return new Memento(this);
  }
  // 根据备忘录对象恢复原发器状态
  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;
     }
} 

负责人类:

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);
   }
}

四、模式例子

实例:用户信息操作撤销
某系统提供了用户信息操作模块,用户可以修改自己的各项信息。为了使操作过程更加人性化,现使用备忘录模式对系统进行改进,使得用户在进行了错误操作之后可以恢复到操作之前的状态。

本例子来自《设计模式》一书

原发器类,创建备忘录类

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);		
	}
}

备忘录类,保存原发器类状态:

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;
	}
}

负责人类,创建备忘录:

package dp.memento;

public class Caretaker
{
	private Memento memento;
	public Memento getMemento()
	{
		return memento;
	}
	public void setMemento(Memento memento)
	{
		this.memento=memento;
	}
}

五、模式应用

  • 软件里的存档操作
  • Windows 里的 ctri + z。
  • IE 中的后退操作
  • 数据库的事务管理

猜你喜欢

转载自blog.csdn.net/u014427391/article/details/88598079