行为型模式之五——备忘录模式

模式定义

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

模式结构

备忘录模式结构图如下所示:
这里写图片描述

  1. Originator
    原发器可以创建一个备忘录,并存储它的当前内部状态,也可以使用备忘录来恢复其内部状态。一般将需要保存内部状态的类设计为原发器。

  2. Memento
    存储原发器的内部状态,根据原发器来决定保存哪些内部状态。备忘录的设计一般可以参考原发器的设计,根据实际需要确定备忘录类中的属性。需要注意的是,除了原发器本身与负责人类之外,备忘录对象不能直接供其他类使用,因此原发器的设计在不同的编程语言中实现机制有所区别。

  3. Caretaker
    负责人又称为管理者,它负责保存备忘录,但是不能对备忘录的内容进行操作或检查。在负责人类中可以存储一个或多个备忘录对象,它负责存储对象,而不能修改对象,也无须知道对象的实现细节。

实例

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

package dp.memnto;

public class UestInfoDTO {
    private String account;
    private String password;
    private String telNO;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return this.password;
    }

    public String getTelNO() {
        return this.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 = memennto.getAccount();
        this.password = memento.getPassword();
        this.telNO = memento.getTelNO();
    }

    public void show() {
        System.out.println("Accout: " + account);
        System.out.println("Password: " + password);
        System.out.println("TelNO: " + 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 void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return this.password;
    }

    public String getTelNO() {
        return this.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;
    }
}

import dp.memento.*;

public class Client {
    public static void main(String[] args) {
        UserInfoDTO user = new UserInfoDTO();

        Caretaker c = new Caretaker();

        user.setAccount("张三");
        user.setPassword("123456");
        user.setTelNO("888888888");
        System.out.println("状态一:");
        user.show();
        c.setMemento(user.saveMemento());
        System.out.println("--------------------------");

        user.setPassword("654987");
        user.setTelNO("666666666");
        System.out.println("状态二:");
        user.show();

        user.restoreMemento(c.getMemento());
        System.out.println("回到状态一");
        System.out.println("--------------------------");
        user.show();
        System.out.println("--------------------------");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值