备忘录模式 
备忘录模式是对象行为型模式,也称快照模式,在不破坏封装性的前提下,捕获一个对象状态并保存,而且可以在需要的时候,进行还原。  
在许多游戏里,我们都会有存档功能,在存档过程中进行状态保存。还有许多的软件都有撤销功能,除了对命令的储存,还有对当前状态的保存。  实现状态保存并不难,但我们需要对保存对象的字段进行保护的前提下,还可以对其进行保存。这就是黑箱。  
这里使用 双接口 的形式来完成对不同类的访问。  
 
 双接口  
 - 窄接口:除了原发器以外的对象都是通过窄接口获得对象的,这个接口只能转移对象,无法对对象进行修改。
 - 宽接口:与窄接口不同,宽接口可以访问对象中的所有信息,允许读取。
   
  
结构     说明  
- 原发器(Originator) - 保护自身状态,且可以通过快照生成备忘录类来保存自身状态,也可以通过快照恢复自身状态。
 - 备忘录(Memento) - 一个不可变的值对象(只读),通过构造创建新对象。保存一个快照。
 - 负责人(Caretaker)- 负责记录快照,并保留,需要时进行发送快照给原发器进行恢复状态,可以管理快照,但无法修改。
 - 窄接口(I Memento) - 作为标识符,没有任何方法,负责人通过窄接口(黑箱)来进行传递。
   
实现 
保存游戏存档的功能  使用的是 SteamAssets文件夹  
玩家信息类(原发器) 和 备忘录(嵌套)  
    public class PlayerInfo : MonoBehaviour
    {
        private FileSave _save;
        [SerializeField] private string _name;
        [SerializeField] private int _grade;
        [SerializeField] private int _attackForce;
        [SerializeField] private float _hp;
        private void Awake()
        {
            _save = new FileSave();
        }
		
		
        public void Save()
        {
            PlayerStateMemento memento = 
                new PlayerStateMemento(_name, _grade, _attackForce, _hp);
            string json = JsonUtility.ToJson(memento, true);
            _save.Save(json);
        }
        public void Restore()
        {
            string json = _save.ReadArchive();
            PlayerStateMemento memento = 
                JsonUtility.FromJson<PlayerStateMemento>(json);
            _name = memento.Name;
            _grade = memento.Grade;
            _attackForce = memento.AttackForce;
            _hp = memento.Hp;
        }
		
		
        [Serializable]
        private class PlayerStateMemento
        {
            [SerializeField] private string _name;
            [SerializeField] private int _grade;
            [SerializeField] private int _attackForce;
            [SerializeField] private float _hp;
            public string Name => _name;
            public int Grade => _grade;
            public int AttackForce => _attackForce;
            public float Hp => _hp;
            
            public PlayerStateMemento(string name, int grade, int attackForce, float hp)
            {
                _name = name;
                _grade = grade;
                _attackForce = attackForce;
                _hp = hp;
            }
        }
    }
  
文件保存类(负责人)  
    public class FileSave
    {
        private string _path;
        public FileSave()
        {
            CreateSaveFile();
        }
        public void Save(string json)
        {
            File.WriteAllText(_path, json);
        }
        public string ReadArchive()
        {
            return File.ReadAllText(_path);
        }
        private void CreateSaveFile()
        {
            if (!string.IsNullOrEmpty(_path)) return;
            _path = Application.streamingAssetsPath;
            if (!Directory.Exists(_path))
                Directory.CreateDirectory(_path);
                
            _path = Path.Combine(_path, "Save.json");
                
            if (!File.Exists(_path))
                File.Create(_path);
        }
    }
  
通过两个按钮进行测试        
 
 读档成功,以下是 Json 文件     
  
应用场景 
- 当你需要记录对象的状态时,并且希望可以恢复之前的状态。
 - 当你需要通过双接口来获得不同的访问权限时
   
优缺点 
优点  
- 可以在不破坏封装性的前提下,创建对象状态快照
 - 可以维护快照,来还原之前的状态
  缺点 - 过于频繁的创建快照,可能会消耗大量的内存
   
与其他模式关系 
- 同时使用命令模式和备忘录模式来实现 撤销 的功能,在这种情况下,命令用于对象的操作,备忘录用于还原之前的状态。
   
 
 在不支持嵌套类的语言中  我们可以使用两个接口来实现双接口操作  
 
                
                
                
        
        
    
  
 
 |