设计模式 备忘录模式


概念


案例

白箱模式

创建role类:

@Data
public class GameRole {
    private int vit;
    private int atk;
    private int def;

    public void init(){
        this.vit = 100;
        this.atk = 100;
        this.def = 100;
    }

    //战斗
    public void fight(){
        this.vit = 0;
        this.atk = 0;
        this.def = 0;
    }

    public void display(){
        System.out.println("生命 " + this.vit);
        System.out.println("攻击" + this.atk);
        System.out.println("防御" + this.def);
    }

    //保存角色状态
    public RoleStateMemento saveState(){
        return new RoleStateMemento(vit,atk,def);
    }

    //回复状态
    public void recoverState(RoleStateMemento role){
        this.vit = role.getVit();
        this.atk = role.getAtk();
        this.def = role.getDef();
    }
}

备忘录类:

//备忘录类
@Data
@AllArgsConstructor
public class RoleStateMemento {
    private int vit;
    private int atk;
    private int def;
}

状态管理类:

@Data
@AllArgsConstructor
public class RoleStateManager {
    private RoleStateMemento role;
}

测试使用:

public class Client {
    public static void main(String[] args) {
        System.out.println("打BOSS前----");
        //打BOSS前
        GameRole role = new GameRole();
        role.init();
        role.display();

        //保存
        RoleStateManager manager = new RoleStateManager(role.saveState());

        //打BOSS 打不过
        System.out.println("打BOSS后----");
        role.fight();
        role.display();

        //回滚
        System.out.println("读档----");
        role.recoverState(manager.getRole());
        role.display();
    }
}

黑箱模式


通过吧备忘录类私有并定义在role类内部
使得只有role类能去操作数据,外部类通过一个标识接口访问状态类
定义一个标识接口 没有实质内容

public interface Memento {
}

定义role类 内部私有状态类

@Data
public class GameRole {
    private int vit;
    private int atk;
    private int def;

    public void init(){
        this.vit = 100;
        this.atk = 100;
        this.def = 100;
    }

    //战斗
    public void fight(){
        this.vit = 0;
        this.atk = 0;
        this.def = 0;
    }

    public void display(){
        System.out.println("生命 " + this.vit);
        System.out.println("攻击" + this.atk);
        System.out.println("防御" + this.def);
    }

    //保存角色状态
    public RoleStateMemento saveState(){
        return new RoleStateMemento(vit,atk,def);
    }

    //回复状态
    public void recoverState(Memento memento){
        RoleStateMemento role = (RoleStateMemento) memento;
        this.vit = role.getVit();
        this.atk = role.getAtk();
        this.def = role.getDef();
    }

    @AllArgsConstructor
    @Data
    private class RoleStateMemento implements Memento{
        private int vit;
        private int atk;
        private int def;
    }
}

定义管理状态类 只能访问标识接口 使得无法修改数据

@AllArgsConstructor
@Data
//这里只拿到标识接口 因此管理者类无法去修改数据
public class RoleStateManager {
    private Memento memento;
}

测试使用:

public class Client {
    public static void main(String[] args) {
        System.out.println("打BOSS前----");
        //打BOSS前
        GameRole role = new GameRole();
        role.init();
        role.display();

        //保存
        RoleStateManager manager = new RoleStateManager(role.saveState());

        //打BOSS 打不过
        System.out.println("打BOSS后----");
        role.fight();
        role.display();

        //回滚
        System.out.println("读档----");
        role.recoverState(manager.getMemento());
        role.display();
    }
}

总结