设计模式——备忘录模式(Memento)

文章目录

  • 备忘录设计模式(看视频做记录示例)
    • 0. UML结构图
    • 1. 看视频做记录示例 Code
      • 1.1 视频
      • 1.2 视频备份
      • 1.3 视频备份维护的负责人
    • 2. 看视频做记录示例 Test

代码:GitHub

备忘录设计模式(看视频做记录示例)

0. UML结构图

设计模式——备忘录模式(Memento)_第1张图片

1. 看视频做记录示例 Code

1.1 视频

public class Video {

    private String name;

    private int size;

    // 播放进度
    private int progress;

    public Video(String name, int size, int progress) {
        this.name = name;
        this.size = size;
        this.progress = progress;
    }

    public String getName() {
        return name;
    }

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

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    /**
     * 备份
     */
    public VideoMemento memento() {
        return new VideoMemento(this);
    }

    /**
     * 从备份处恢复
     *
     * @param memento 备份
     */
    public void recovery(VideoMemento memento) {
        this.name = memento.getName();
        this.size = memento.getSize();
        this.progress = memento.getProgress();
    }

    @Override
    public String toString() {
        return "Video{" +
                "name='" + name + '\'' +
                ", size=" + size +
                ", progress=" + progress +
                '}';
    }

}

1.2 视频备份

public class VideoMemento {

    private String name;

    private int size;

    // 播放进度
    private int progress;

    public VideoMemento(Video video) {
        this.name = video.getName();
        this.size = video.getSize();
        this.progress = video.getProgress();
    }

    public String getName() {
        return name;
    }

    public int getSize() {
        return size;
    }

    public int getProgress() {
        return progress;
    }

}

1.3 视频备份维护的负责人

public class CareTaker {

    private Map<String, VideoMemento> map = new HashMap<>();

    public void setMemento(String key, VideoMemento memento) {
        map.put(key, memento);
    }

    public VideoMemento getMemento(String key) {
        return map.get(key);
    }

}

2. 看视频做记录示例 Test

public class MementoTest {

    public static void main(String[] args) {
        CareTaker careTaker = new CareTaker();

        Video video = new Video("Titanic", 100, 0);
        System.out.println(video);

        System.out.println("开始看电影");
        for (int i = video.getProgress(); i <= 70; i += 10) {
            video.setProgress(i);
            System.out.println("电影播放了" + i + "%了");
        }
        System.out.println(video);

        System.out.println("突然有事,需要出去,先备份一下当前记录");
        VideoMemento memento = video.memento();
        careTaker.setMemento("high tide", memento);

        for (int i = video.getProgress() + 10; i <= 100; i += 10) {
            video.setProgress(i);
            System.out.println("电影播放了" + i + "%了");
        }
        System.out.println(video);

        System.out.println("回家了,继续看Titanic,从备份记录处恢复");

        video.recovery(careTaker.getMemento("high tide"));
        System.out.println(video);

    }

}

你可能感兴趣的:(Java,DesignPatterns)