C# 备忘录设计模式 设计模式

请添加图片描述

1、发起者

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Memento
{
    /// <summary>
    /// 角色 (发起者 备忘对象)
    /// </summary>
    public class RoleOriginatory
    {
        /// <summary>
        /// 生命力 100
        /// </summary>
        public int Hp { get; set; }

        /// <summary>
        /// 攻击力 
        /// </summary>
        public int Attack { get; set; }

        public RoleOriginatory()
        {
            Hp = 100;
            Attack = 50;
        }

        public override string ToString()
        {
            string strShow = "--- 生命力:" + Hp.ToString() + "\n    攻击力:" + Attack.ToString();
            return strShow;
        }

        /// <summary>
        /// 攻击
        /// </summary>
        public void Fight()
        {
            this.Attack += 2;
            this.Hp -= 10;
        }

        /// <summary>
        /// 保存快照
        /// </summary>
        /// <returns></returns>
        public RoleMemento SaveState()
        {
            return new RoleMemento(this.Hp, this.Attack);
        }

        /// <summary>
        /// 恢复快照
        /// </summary>
        public void RecoveryState(RoleMemento memento)
        {
            this.Hp = memento.Hp;
            this.Attack = memento.Attack;
        }

    }
}

2、备忘录

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Memento
{
    /// <summary>
    /// 角色的备忘录 
    /// </summary>
    public class RoleMemento
    {
        /// <summary>
        /// 生命力 100
        /// </summary>
        public int Hp { get; set; }

        /// <summary>
        /// 攻击力 
        /// </summary>
        public int Attack { get; set; }

        public RoleMemento(int hp, int attack)
        {
            this.Hp = hp;
            this.Attack = attack;
        }
    }
}

3、备忘录管理者

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Memento
{
    /// <summary>
    /// 角色管理者
    /// </summary>
    public class RoleCaretaker
    {
        private RoleMemento memento;

        public RoleMemento GetRoleMemento()
        {
            return this.memento;
        }

        public void SetRoleMemento( RoleMemento memento)
        {
            this.memento = memento;
        }
    }
}

4、使用备忘录

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Memento
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("备忘录设计模式");

            // 创建发起者
            RoleOriginatory role = new RoleOriginatory();

            Console.WriteLine(role.ToString());

            // 攻击
            role.Fight();
            Console.WriteLine("攻击-----------------");
            Console.WriteLine(role.ToString());

            // 当前状态 存档
            Console.WriteLine("存档-----------------");
            RoleCaretaker roleCaretaker = new RoleCaretaker();
            roleCaretaker.SetRoleMemento(role.SaveState());

            // 存档后攻击
            role.Fight();
            role.Fight();
            role.Fight();
            role.Fight();
            Console.WriteLine("多次攻击-----------------");
            Console.WriteLine(role.ToString());

            // 恢复快照
            Console.WriteLine("恢复快照-----------------");
            role.RecoveryState(roleCaretaker.GetRoleMemento());
            Console.WriteLine(role.ToString());

            Console.ReadLine();
        }
    }
}

在这里插入图片描述

优缺点

优点

  • 提供了状态的保存机制
  • 实现了信息的封装,只需要调用存档与恢复即可

缺点

  • 消耗更多的资源
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

廷益--飞鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值