[AYJS] wpf设计模式学习[13]-备忘录模式

场景:用于还原上一次存储的对象的状态。

结构图:

blob.png

这里Ay直接拿 命令的方式去写,我们经常遇到Ctrl+Z撤销命令,还原到上次,为了让模式更好懂,我就简单写了。

第一步:AyCommandManager担当Caretaker角色,AyCommandClonePropery担当Memnto角色,用于存储Originator的值

    public class AyCommandManager
    {
        private AyCommandClonePropery command;

        public AyCommandClonePropery Command
        {
            get { return command; }
            set { command = value; }
        }

    }

     public class AyCommandClonePropery
    {
        private string commandName;

        public string CommandName
        {
            get { return commandName; }
            set { commandName = value; }
        }

        private Action commandAction;

        public Action CommandAction
        {
            get { return commandAction; }
            set { commandAction = value; }
        }
    }

第二步:新增Originator角色,也就是希望被还原的属性和方法

  public class AyCommand
    {
        private string commandName;
        public string CommandName
        {
            get { return commandName; }
            set { commandName = value; }
        }

        private Action commandAction;

        public Action CommandAction
        {
            get { return commandAction; }
            set { commandAction = value; }
        }

        public AyCommandClonePropery CreateAyCommand()
        {
            AyCommandClonePropery cmd = new AyCommandClonePropery();
            cmd.CommandAction = CommandAction;
            cmd.CommandName = CommandName;
            return cmd;
        }
        public void SetAyCommand(AyCommandClonePropery cmd)
        {
            this.CommandName = cmd.CommandName;
            this.CommandAction = cmd.CommandAction;
        }


        public void ExecuteAction()
        {
            Console.WriteLine(CommandName+"开始执行:");
            CommandAction();
        }



    }

在这里,AyCommandClonePropery是不需要对Client端暴露的,客户端只关注其他的2个类

客户端使用:

            AyCommand cmd = new AyCommand();
            cmd.CommandName="粘贴";
            cmd.CommandAction = () =>
            {
                Console.WriteLine("执行了粘贴功能");
            };

            cmd.ExecuteAction();

运行界面:

blob.png

客户端在不知情的情况下,以为我们没有增加其他功能呢,其实我们增加了备份功能。

     //备忘录模式
            AyCommand cmd = new AyCommand();
            cmd.CommandName="粘贴";
            cmd.CommandAction = () =>
            {
                Console.WriteLine("执行了粘贴功能");
            };

            cmd.ExecuteAction();

            //备份
            AyCommandManager cmdManager = new AyCommandManager();
            cmdManager.Command = cmd.CreateAyCommand();

            cmd.CommandAction = () =>
            {
                Console.WriteLine("粘贴功能,粘贴失败!");
            };

            cmd.ExecuteAction();

            Console.WriteLine();
            Console.WriteLine("还原后,执行上一次的命令");
            //还原
            cmd.SetAyCommand(cmdManager.Command);
            cmd.ExecuteAction();

效果图:

blob.png

总结:通过一个中间类,备份我们给客户提供搞得类的属性,通过给客户提供的2个额外的方法,备份和还原属性,实现备份的效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值