Memento备忘录模式

顾名思义,可以用来储存数据


using UnityEngine;
using System.Collections.Generic;


namespace MementoExample2
{


    public class MementoExample2 : MonoBehaviour
    {
        //存储器
        Caretaker caretaker = new Caretaker();
        //发起
        Originator originator = new Originator();

        int savedFiles = 0, currentArticle = 0;

        void Start()
        {
            // 存储一些内容
            Save("版本1");
            Save("版本2");
            Save("版本3");
            Save("版本4");

            // 回退或者继续
            Debug.Log("Pressing Undo");
            Undo();
            Debug.Log("Pressing Undo");
            Undo();
            Debug.Log("Pressing Undo");
            Undo();
            Debug.Log("Pressing Redo");
            Redo();
        }


        public void Save(string text)
        {
            originator.Set(text);
            caretaker.Add(originator.StoreInMemento());
            savedFiles = caretaker.GetCountOfSavedArticles();
            currentArticle = savedFiles;
        }

        public string Undo()
        {
            if (currentArticle > 0)
                currentArticle -= 1;

            Memento prev = caretaker.Get(currentArticle);
            string prevArticle = originator.RestoreFromMemento(prev);
            return prevArticle;
        }

        public string Redo()
        {
            if (currentArticle < savedFiles)
                currentArticle += 1;

            Memento next = caretaker.Get(currentArticle);
            string nextArticle = originator.RestoreFromMemento(next);
            return nextArticle;
        }

    }




    /// <summary>
    /// 结构简单,就一行字符串
    /// </summary>
    public class Memento
    {
        public string article { get; protected set; }

        public Memento(string article)
        {
            this.article = article;
        }
    }


    /// <summary>
    /// 发起内容,存储器,两个方法
    /// </summary>
    public class Originator
    {
        public string article { get; protected set; }

        public void Set(string article)
        {
            Debug.Log("存档中: [\"" + article + "\"]");
            this.article = article;
        }

        public Memento StoreInMemento()
        {
            Debug.Log("存档完成: [\"" + this.article + "\"]");
            return new Memento(this.article);
        }

        public string RestoreFromMemento(Memento memento)
        {
            article = memento.article;
            Debug.Log("SL中: [\"" + article + "\"]");
            return article;
        }
    }


    /// <summary>
    /// 存储所有内容,三个方法
    /// </summary>
    public class Caretaker
    {
        List<Memento> savedArticles = new List<Memento>();

        public void Add(Memento m)
        {
            savedArticles.Add(m);
        }

        public Memento Get(int i)
        {
            return savedArticles[i];
        }

        public int GetCountOfSavedArticles()
        {
            return savedArticles.Count;
        }
    }


}
发布了91 篇原创文章 · 获赞 12 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lvcoc/article/details/87872811