Design Pattern Memo Mode Notes

illustrate

Record the writing method of learning design pattern-memo pattern. The JDK version used is version 1.8.

Memento(Memo)

Intent : Capture the internal state of an object and save this state outside the object without breaking encapsulation. This allows you to later restore the object to its original saved state.
Structure :
Insert image description here

in:

  • Memento (memo) stores the internal state of the originator object. The originator determines which internal state of the originator the memento stores as needed: preventing other objects other than the originator from accessing the memento.
  • The Originator creates a memo to record its internal state at the current moment; use the memo to restore the internal state.
  • Caretaker (manager) is responsible for saving the memo; he cannot operate or check the content of the memo.

applicability:

  • Scenarios where data needs to be saved and restored, such as the archiving function of intermediate results when playing games.
  • It is necessary to provide a scenario where the rollback operation can be performed, such as pressing the Ctrl + key combination during editing in Word Notepad, Photoshop, Idea and other software, as well as transaction operations in the database.

Table of contents

Insert image description here

White box memo mode

Memo pattern example class diagram

Insert image description here
Insert image description here

Use this UML class diagram to implement the white box memo pattern example.

Game characters

package com.example.deesign_patterns.memento.white_box;

//游戏角色类(属于发起人角色)
public class GameRole {
    
    

    private int vit;//生命力
    private int atk;//攻击力
    private int def;//防御力

    //初始化内部状态
    public void initState(){
    
    
        this.vit=100;
        this.atk=100;
        this.def=100;
    }

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

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

    //恢复角色状态
    public void recoverState(RoleStateMemento roleStateMemento){
    
    
        //将备忘录对象中存储的状态赋值给当前对象的成员
        this.vit=roleStateMemento.getVit();
        this.atk=roleStateMemento.getAtk();
        this.def=roleStateMemento.getDef();
    }

    //展示状态功能
    public void stateDisplay(){
    
    
        System.out.println("角色生命力:"+vit);
        System.out.println("角色攻击力:"+atk);
        System.out.println("角色防御力:"+def);
    }

    public int getVit() {
    
    
        return vit;
    }

    public void setVit(int vit) {
    
    
        this.vit = vit;
    }

    public int getAtk() {
    
    
        return atk;
    }

    public void setAtk(int atk) {
    
    
        this.atk = atk;
    }

    public int getDef() {
    
    
        return def;
    }

    public void setDef(int def) {
    
    
        this.def = def;
    }
}

Memo role class

package com.example.deesign_patterns.memento.white_box;

//备忘录角色类
public class RoleStateMemento {
    
    

    private int vit;//生命力
    private int atk;//攻击力
    private int def;//防御力

    public RoleStateMemento() {
    
    
    }

    public RoleStateMemento(int vit, int atk, int def) {
    
    
        this.vit = vit;
        this.atk = atk;
        this.def = def;
    }

    public int getVit() {
    
    
        return vit;
    }

    public void setVit(int vit) {
    
    
        this.vit = vit;
    }

    public int getAtk() {
    
    
        return atk;
    }

    public void setAtk(int atk) {
    
    
        this.atk = atk;
    }

    public int getDef() {
    
    
        return def;
    }

    public void setDef(int def) {
    
    
        this.def = def;
    }
}

Memo object management object

package com.example.deesign_patterns.memento.white_box;

//备忘录对象管理对象
public class RoleStateCaretaker {
    
    

    //声明RoleStateMemento类型的变量
    private RoleStateMemento roleStateMemento;

    public RoleStateMemento getRoleStateMemento() {
    
    
        return roleStateMemento;
    }

    public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
    
    
        this.roleStateMemento = roleStateMemento;
    }
}

Test class

package com.example.deesign_patterns.memento.white_box;

//测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        System.out.println("-----------大战boos前-------------");
        //创建游戏角色对象
        GameRole gameRole=new GameRole();
        gameRole.initState();
        gameRole.stateDisplay();
        //将该游戏角色内部状态进行备份
        //创建管理者对象
        RoleStateCaretaker roleStateCaretaker=new RoleStateCaretaker();
        roleStateCaretaker.setRoleStateMemento(gameRole.saveState());

        System.out.println("-----------大战boos后-------------");
        //损耗严重
        gameRole.fight();
        gameRole.stateDisplay();

        System.out.println("-----------恢复之前的状态-------------");
        gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());
        gameRole.stateDisplay();
    }
}

Insert image description here

Black box memo mode

Memo pattern example class diagram

The memento role provides a wide interface to the initiator object and a narrow interface to other objects. In the Java language, the way to implement dual interfaces is to design the memo class as an internal member class that initiates humans.

Set RoleStateMemento as the internal class of GameRole, thereby encapsulating the RoleStateMemento object in Gamerole: providing an identification interface Memento outside for Rolestatecaretaker and its objects to use. In this way, the Gamerole class sees all the interfaces of RoleStateMemento, while other objects only see the interfaces exposed by the identification interface Memento, thus maintaining the encapsulation type. The class diagram is as follows:
Insert image description here

Use this UML class diagram to implement the black box memo pattern example.

Memo interface

package com.example.deesign_patterns.memento.black_box;

//备忘录接口,对外提供窄接口
public interface Memento {
    
    
}

Game characters

package com.example.deesign_patterns.memento.black_box;

//游戏角色类(属于发起人角色)
public class GameRole {
    
    

    private int vit;//生命力
    private int atk;//攻击力
    private int def;//防御力

    //初始化内部状态
    public void initState(){
    
    
        this.vit=100;
        this.atk=100;
        this.def=100;
    }

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

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

    //恢复角色状态
    public void recoverState(Memento memento){
    
    
        RoleStateMemento roleStateMemento= (RoleStateMemento) memento;
        //将备忘录对象中存储的状态赋值给当前对象的成员
        this.vit=roleStateMemento.getVit();
        this.atk=roleStateMemento.getAtk();
        this.def=roleStateMemento.getDef();
    }

    //展示状态功能
    public void stateDisplay(){
    
    
        System.out.println("角色生命力:"+vit);
        System.out.println("角色攻击力:"+atk);
        System.out.println("角色防御力:"+def);
    }

    public int getVit() {
    
    
        return vit;
    }

    public void setVit(int vit) {
    
    
        this.vit = vit;
    }

    public int getAtk() {
    
    
        return atk;
    }

    public void setAtk(int atk) {
    
    
        this.atk = atk;
    }

    public int getDef() {
    
    
        return def;
    }

    public void setDef(int def) {
    
    
        this.def = def;
    }

    //声明一个内部类
    private class RoleStateMemento implements Memento{
    
    
        private int vit;//生命力
        private int atk;//攻击力
        private int def;//防御力

        public RoleStateMemento() {
    
    
        }

        public RoleStateMemento(int vit, int atk, int def) {
    
    
            this.vit = vit;
            this.atk = atk;
            this.def = def;
        }

        public int getVit() {
    
    
            return vit;
        }

        public void setVit(int vit) {
    
    
            this.vit = vit;
        }

        public int getAtk() {
    
    
            return atk;
        }

        public void setAtk(int atk) {
    
    
            this.atk = atk;
        }

        public int getDef() {
    
    
            return def;
        }

        public void setDef(int def) {
    
    
            this.def = def;
        }
    }
}

Memo object management object

package com.example.deesign_patterns.memento.black_box;


//备忘录对象管理对象
public class RoleStateCaretaker {
    
    

    //声明Memento类型的变量
    private Memento memento;

    public Memento getMemento() {
    
    
        return memento;
    }

    public void setMemento(Memento memento) {
    
    
        this.memento = memento;
    }
}

Test class

package com.example.deesign_patterns.memento.black_box;

//测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        System.out.println("-----------大战boos前-------------");
        //创建游戏角色对象
        GameRole gameRole=new GameRole();
        gameRole.initState();
        gameRole.stateDisplay();
        //将该游戏角色内部状态进行备份
        //创建管理者对象
        RoleStateCaretaker roleStateCaretaker=new RoleStateCaretaker();
        roleStateCaretaker.setMemento(gameRole.saveState());

        System.out.println("-----------大战boos后-------------");
        //损耗严重
        gameRole.fight();
        gameRole.stateDisplay();

        System.out.println("-----------恢复之前的状态-------------");
        gameRole.recoverState(roleStateCaretaker.getMemento());
        gameRole.stateDisplay();
    }
}

Insert image description here

benefit:

  • Provides a mechanism to restore state. When users need it, they can more easily restore data to a certain historical state.
  • Implemented the encapsulation of internal state. This state information is not accessible to other objects except the originator who created it.
  • Simplified initiating humans. The initiator does not need to manage and save individual backups of its internal state. All state information is saved in memos and managed by managers, which is consistent with the single responsibility principle.

shortcoming:

  • Resource consumption is large. If there is too much internal state information to be saved or it is very frequent, it will occupy relatively large memory resources.

Guess you like

Origin blog.csdn.net/weixin_48040732/article/details/131371376