java设计模式——备忘录模式

一、备忘录模式的定义与类型

1、定义

  • 保存一个对象的某个状态,以便在适当的时候回复对象

2、类型

  • 行为型

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

  • 保存及恢复数据相关业务场景

三、备忘录模式的优点

  • 为用户提供一种可恢复机制
  • 存档信息的封装

四、备忘录模式的缺点

  • 资源占用

五、备忘录模式相关设计模式

  • 备忘录模式和状态模式

六、备忘录模式示例

以网站上发布笔记,并把笔记暂时存储为例进行代码演示

1、代码结构如下:
在这里插入图片描述

2、定义一个笔记类

package com.rf.designPatterns.behavioral.memo;

/**
 * @description: 定义一个笔记类
 * @author: xz
 */
public class Notes {
    private String title;//标题
    private String content;//内容
    private String image;//图片
    //构造方法
    public Notes(String title, String content, String image) {
        this.title = title;
        this.content = content;
        this.image = image;
    }

    //getter、setter方法
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    //重写toString方法
    @Override
    public String toString() {
        return "Notes{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", image='" + image + '\'' +
                '}';
    }
    /**
     *保存笔记到备忘录方法
     */
    public NotesMeno saveToMeno(){
        NotesMeno notesMeno = new NotesMeno(this.title, this.content, this.image);
        return notesMeno;
    }

    /**
     * 从备忘录中回退笔记的方法
     */
    public void undoFromMeno(NotesMeno notesMeno){
        this.title=notesMeno.getTitle();
        this.content=notesMeno.getContent();
        this.image=notesMeno.getImage();
    }

}

3、定义一个笔记的备忘录类,类似于快照

package com.rf.designPatterns.behavioral.memo;

/**
 * @description: 定义一个笔记的备忘录类,类似于快照
 * @author: xz
 */
public class NotesMeno {
    private String title;//标题
    private String content;//内容
    private String image;//图片
    //构造方法
    public NotesMeno(String title, String content, String image) {
        this.title = title;
        this.content = content;
        this.image = image;
    }
    //getter方法
    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }

    public String getImage() {
        return image;
    }
    //重写toString方法
    @Override
    public String toString() {
        return "NotesMeno{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", image='" + image + '\'' +
                '}';
    }
}

4、定义一个笔记备忘录管理类

package com.rf.designPatterns.behavioral.memo;

import java.util.Stack;

/**
 * @description: 定义一个笔记备忘录管理类
 * @author: xz
 */
public class NotesMenoManager {
    //定义一个栈
    private final Stack<NotesMeno> NOTES_MENO_STACK = new Stack<>();

    /**
     * 获取备忘录方法
     */
    public NotesMeno getMeno(){
        NotesMeno notesMeno = NOTES_MENO_STACK.pop();
        return notesMeno;
    }

    /**
     * 存储备忘录方法
     */
    public void addMeno(NotesMeno notesMeno){
        NOTES_MENO_STACK.push(notesMeno);
    }

}

5、定义一个备忘录设计模式的测试类

package com.rf.designPatterns.behavioral.memo;

/**
 * @description: 定义一个备忘录设计模式的测试类
 * @author: xz
 */
public class Test {
    public static void main(String[] args) {
        //实例化一个笔记备忘录管理
        NotesMenoManager notesMenoManager = new NotesMenoManager();
        //创建笔记
        Notes notes = new Notes("备忘录设计模式课程A",
                                "备忘录设计模式内容A",
                                "备忘录设计模式UML类图A");
        //保存笔记到备忘录
        NotesMeno notesMeno = notes.saveToMeno();
        //存储备忘录
        notesMenoManager.addMeno(notesMeno);
        System.out.println("笔记暂存成功!");
        System.out.println("笔记的完整信息"+notes);


        System.out.println("-------修改笔记 start-----------");
        notes.setTitle("备忘录设计模式课程B");
        notes.setContent("备忘录设计模式内容B");
        notes.setImage("备忘录设计模式UML类图B");
        System.out.println("-------修改笔记 end-----------");
        System.out.println("修改后的笔记的完整信息"+notes);
        //保存笔记到备忘录
        NotesMeno notesMeno1 = notes.saveToMeno();
        //存储备忘录
        notesMenoManager.addMeno(notesMeno1);
        System.out.println("修改笔记后暂存成功!");

        System.out.println("笔记暂存回退 start");
        System.out.println("回退出栈第一次");
        notesMeno = notesMenoManager.getMeno();
        notes.undoFromMeno(notesMeno);
        System.out.println("回退出栈第二次");
        notesMeno = notesMenoManager.getMeno();
        notes.undoFromMeno(notesMeno);
        System.out.println("笔记暂存回退 end");

        System.out.println("笔记的完整信息:"+notes);
    }
}

6、2次暂存笔记后,笔记管理类中已有2个对象,如下图:
在这里插入图片描述
7、回退第二次开始的时候,笔记管理类中还剩1个对象,如下图:
在这里插入图片描述

8、运行测试类,输出结果如下:
在这里插入图片描述
9、UML类图如下:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小志的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值