⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mementos.cs

📁 从设计模式角度介绍了updo redo的实现方法
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using GenericUndoRedo;
using ShapeLib;

namespace UndoRedoByActionDemo
{
    abstract class ShapeMemento : IMemento<ShapePool>
    {
        #region IMemento<ShapePool> Members

        public abstract IMemento<ShapePool> Restore(ShapePool target);
        
        #endregion
    }
    
    class InsertShapeMemento : ShapeMemento
    {
        private int index;
        public InsertShapeMemento(int index)
        {
            this.index = index;
        }

        public override IMemento<ShapePool> Restore(ShapePool target)
        {
            Shape removed = target[index];
            IMemento<ShapePool> inverse = new RemoveShapeMemento(index, removed);
            target.RemoveAt(index);
            return inverse;
        }
    }
    
    class RemoveShapeMemento : ShapeMemento
    {
        Shape removed;
        int index;
        public RemoveShapeMemento(int index, Shape removed)
        {
            this.index = index;
            this.removed = removed;
        }

        public override IMemento<ShapePool> Restore(ShapePool target)
        {
            IMemento<ShapePool> inverse = new InsertShapeMemento(index);
            target.Insert(index, removed);
            return inverse;
        }
    }

    class AddShapeMemento : ShapeMemento
    {
        public AddShapeMemento()
        {
        }

        public override IMemento<ShapePool> Restore(ShapePool target)
        {
            int index = target.Count - 1;
            IMemento<ShapePool> inverse = new RemoveShapeMemento(index, target[index]);
            target.RemoveAt(target.Count - 1);
            return inverse;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -