用户信息操作撤销(备忘录模式实战)

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

运行结果截图:

用户信息操作撤销(备忘录模式实战)_第2张图片

类图

用户信息操作撤销(备忘录模式实战)_第3张图片

代码

原发器:

package com.song;

public class Originator {
    private String uname;
    private String password;
    private String sex;

    public Memento createMemento(){
        return new Memento(uname,password,sex);
    }
    
    public void restoreMemento(Memento m){
        this.uname=m.getUname();
        this.password=m.getPassword();
        this.sex=m.getSex();
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPassword() {
        return password;
    }

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

    public String getSex() {
        return sex;
    }

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

    @Override
    public String toString() {
        return "Originator{" +
                "uname='" + uname + '\'' +
                ", password='" + password + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

备忘录:

package com.song;

public class Memento {
    private String uname;
    private String password;
    private String sex;
    public Memento(String uname, String password, String sex) {
        this.uname = uname;
        this.password = password;
        this.sex = sex;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPassword() {
        return password;
    }

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

    public String getSex() {
        return sex;
    }

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

负责人:

package com.song;

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

客户类:

package com.song;

public class Client {
    public static void main(String[] args) {
    Originator originator=new Originator();
    Caretaker caretaker=new Caretaker();

    originator.setUname("song");
    originator.setPassword("123456");
    originator.setSex("女");
    caretaker.setMemento(originator.createMemento());
    System.out.println("用户信息:");
    System.out.println(originator);

    originator.setUname("can");
    originator.setPassword("111111111");
    originator.setSex("男");
    System.out.println("\n------------------------------------------------\n");
    System.out.println("修改后的用户信息”");
    System.out.println(originator);

    System.out.println("\n------------------------------------------------\n");
    System.out.println("回复操作之前的状态:");
    originator.restoreMemento(caretaker.getMemento());
    System.out.println(originator);
    }
}

新人报道,求关注,求关注,求关注!!!
可以直接问我要代码

用户信息操作撤销(备忘录模式实战)_第4张图片

你可能感兴趣的:(设计模式)