java设计模式_备忘录模式

一、什么是备忘录模式

Memento模式也叫备忘录模式,是行为模式之 一,它的作用是保存对象的内部状态,并在需要
的时候(undo/rollback)恢复对象以前的状态。

二、备忘录模式的应用场景

如果一个对象需要保存状态并可通过undo或rollback等
操作恢复到以前的状态时,可以使用Memento模式。1)一个类需要保存它的对象的状态(相当于Originator角色)2)设计一个类,该类只是用来保存上述对象的状态(相当于Memento角色)3)需要的时候,Caretaker角色要求Originator返回一个Memento并加以保存4)undo或rollback操作时,通过Caretaker保存的Memento恢复Originator对象的状态

三、备忘录模式的结构

这里写图片描述

四、备忘录模式的角色和职责

Originator(原生者)
需要被保存状态以便恢复的那个对象。 Memento(备忘录)
该对象由Originator创建,主要用来保存Originator的内部状态。 Caretaker(管理者)
负责在适当的时间保存/恢复Originator对象的状态。

//示例1:

public class Person {

    // 姓名
    private String name;
    // 性别
    private String sex;
    // 年龄
    private int age;

    public Person(){}

    public Person(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }
}

public class MainClass {

    public static void main(String[] args) {
        Person per = new Person("张三","男",30);
        //保存内部状态
        Person backup = new Person();

        backup.setName(per.getName());
        backup.setAge(per.getAge());
        backup.setSex(per.getSex());

        System.out.println(per);

        //修改
        per.setAge(20);
        System.out.println(per);

        //回滚 还原
        per.setName(backup.getName());
        per.setAge(backup.getAge());
        per.setSex(backup.getSex());

        System.out.println(per);
    }
}


//Person [name=张三, sex=男, age=30]
//Person [name=张三, sex=男, age=20]
//Person [name=张三, sex=男, age=30]

//示例2:
public class Memento {

    // 姓名
    private String name;
    // 性别
    private String sex;
    // 年龄
    private int age;

    public Memento(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Memento [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }
}

public class Person {

    // 姓名
    private String name;
    // 性别
    private String sex;
    // 年龄
    private int age;

    public Person(){}

    public Person(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }

    //创建一个备份
    public Memento createMemento(){
        return new Memento(name,sex,age);
    }

    //恢复备份,还原
    public void setMemento(Memento memento){
        this.name = memento.getName();
        this.age = memento.getAge();
        this.sex = memento.getSex();
    }
}


public class MainClass {

    public static void main(String[] args) {
        Person per = new Person("张三","男",24);
        Memento memento = per.createMemento();
        System.out.println(per);

        per.setName("李四");
        per.setSex("女");
        per.setAge(25);

        System.out.println(per);

        per.setMemento(memento);
        System.out.println(per);

    }
}


//Person [name=张三, sex=男, age=24]
//Person [name=李四, sex=女, age=25]
//Person [name=张三, sex=男, age=24]


//示例3:
public class Memento {

    // 姓名
    private String name;
    // 性别
    private String sex;
    // 年龄
    private int age;

    public Memento(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Memento [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }
}

public class Memento {

    // 姓名
    private String name;
    // 性别
    private String sex;
    // 年龄
    private int age;

    public Memento(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Memento [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }
}

public class Person {

    // 姓名
    private String name;
    // 性别
    private String sex;
    // 年龄
    private int age;

    public Person(){}

    public Person(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }

    //创建一个备份
    public Memento createMemento(){
        return new Memento(name,sex,age);
    }

    //恢复备份,还原
    public void setMemento(Memento memento){
        this.name = memento.getName();
        this.age = memento.getAge();
        this.sex = memento.getSex();
    }
}
public class MainClass {

    public static void main(String[] args) {
        Person per = new Person("张三","男",24);
//      Memento memento = per.createMemento();
        Caretaker caretaker = new Caretaker();
        caretaker.setMemento(per.createMemento());

        System.out.println(per);

        per.setName("李四");
        per.setSex("女");
        per.setAge(25);

        System.out.println(per);

        per.setMemento(caretaker.getMemento());
        System.out.println(per);

    }
}

//Person [name=张三, sex=男, age=24]
//Person [name=李四, sex=女, age=25]
//Person [name=张三, sex=男, age=24]










1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值