20、面向对象语言的23种设计模式-备忘录模式

本文介绍了备忘录模式的概念及其实现方式,通过游戏存档的例子详细展示了如何使用该模式来保存和恢复对象的状态,适用于需要回滚操作的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、什么是备忘录模式

就跟这名字一样,可以读取任何时候存储的记录。

二、备忘录模式的作用

就像一个后悔药,在以前的单机游戏中应该很常见,在玩的过程中会有存档,这就相当于是备忘录,保存当时的所有可变状态,方便回滚操作。

三、备忘录模式的使用场景

在有需要回滚操作的时候应该能用上,平时应该用的少,暂时想不出来。。

四、如何实现备忘录模式

我这里就以游戏存档为例,红色警戒存档:

 主程序:

namespace Memento
{
    class Program
    {
        static void Main(string[] args)
        {
            Red red = new Red("中国")
            {
                Architecture = "基地",
                Troops = "4辆犀牛,6个动员兵",
                Money = "6000"
            };
            red.Save("开始");
            red.Show();

            //Console.WriteLine("*******************存档1*******************");
            red.Architecture += ",发电厂,采矿场,兵营,重工";
            red.Troops = "6辆犀牛,1辆防空车,6个动员兵";
            red.Money = "1000";
            red.Save("存档1");

            //Console.WriteLine("*******************存档2*******************");
            red.Architecture += ",雷达";
            red.Troops = "10辆犀牛,2辆防空车,10个动员兵";
            red.Money = "1500";
            red.Save("存档2");

            //Console.WriteLine("*******************存档3*******************");
            red.Architecture += ",实验室";
            red.Troops = "2辆犀牛,2辆防空车";
            red.Money = "500";
            red.Save("存档3");

            //Console.WriteLine("*******************存档4*******************");
            red.Architecture += ",核弹";
            red.Troops = "10辆犀牛,2辆防空车,5个动员兵,10条狗";
            red.Money = "100";
            red.Save("存档4");

            Console.WriteLine("");
            Console.WriteLine("*******************开始*******************");
            red.Load("开始");
            red.Show();
            Console.WriteLine("");

            Console.WriteLine("*******************存档1*******************");
            red.Load("存档1");
            red.Show();
            Console.WriteLine("");

            Console.WriteLine("*******************存档2*******************");
            red.Load("存档2");
            red.Show();
            Console.WriteLine("");

            Console.WriteLine("*******************存档3*******************");
            red.Load("存档3");
            red.Show();
            Console.WriteLine("");

            Console.WriteLine("*******************存档4*******************");
            red.Load("存档4");
            red.Show();
        }
    }
}

游戏信息Red.cs:

namespace Memento
{
    public class Red
    {
        public string Country { get; private set; }
        public string Architecture { get; set; }
        public string Troops { get; set; }
        public string Money { get; set; }

        public Red(string country)
        {
            Country = country;
        }

        public void Show()
        {
            Console.WriteLine($"国家:{this.Country}");
            Console.WriteLine($"建筑:{this.Architecture}");
            Console.WriteLine($"部队:{this.Troops}");
            Console.WriteLine($"Resource:{this.Money}");
        }

        public void Save(string name)
        {
            RedMemento memento = new RedMemento(this.Architecture, this.Troops, this.Money);
            KeepFile.Save(name, memento);
        }

        public void Load(string name)
        {
            RedMemento memento = KeepFile.Get(name);

            this.Architecture = memento.Architecture;
            this.Troops = memento.Troops;
            this.Money = memento.Money;
        }
    }
}

游戏可变信息RedMemento.cs:

namespace Memento
{
    public class RedMemento
    {
        public string Architecture { get; set; }
        public string Troops { get; set; }
        public string Money { get; set; }

        public RedMemento(string architecture, string troops, string money)
        {
            this.Architecture = architecture;
            this.Troops = troops;
            this.Money = money;
        }
    }
}

存档KeepFile.cs:

namespace Memento
{
    public class KeepFile
    {
        private static Dictionary<string, RedMemento> _Dictionary = new Dictionary<string, RedMemento>();
        public static void Save(string name, RedMemento redMemento)
        {
            _Dictionary.Add(name, redMemento);
        }

        public static RedMemento Get(string name)
        {
            if (_Dictionary.ContainsKey(name))
                return _Dictionary[name];
            else
                throw new Exception("未找到存档");
        }
    }
}

以上为本章所有内容。

完。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊脑袋_YA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值