C#基本数据设定之备忘录模式

前言

用到这个模式无非就是想要做到撤销刚保存已修改的基本数据信息的一个作用。关于这个功能不用状态模式其实也可以做出来,但在这里为了练手上个项目刚学习的设计模式,于是我选择用了状态模式。

正文

备忘录模式即是定义一个临时存储的地方,用来存储对象的对象。在这里使用就是你可以存在实体层里也可以建一个数据库表存着。
备忘录模式主要内容如下:
在这里插入图片描述
Originator(发起人):负责创建一个备忘录Memento,记录当前时刻它的内部状态,并可使用备忘录使用备忘录恢复内部状态

                                 发起人可决定备忘录的内部状态。

Memento(备忘录):负责存储发起人的内部状态,防止除发起人之外的对象访问备忘录

Caretaker(管理者):负责保存好备忘录,但不能进行操作

备忘录管理层

 //备忘录管理者类:复制保存好备忘录
    public class BasicCaretaker
    {
        //定义备忘录类型字段
        //管理看到备忘录的接口,将值传出去
        private BasicStateMemento memento;

        public BasicStateMemento Memento
        {
            get { return memento; }
            set { memento = value; }
        }
    }

发起人

//发起人,发起人可决定备忘录的内部状态
    public class BasicDataEnitity
    {
        //字段
        private string temporatyRate;
        private string fixeduserFee;
        private string unitTime;
        private string leastTime;
        private string preTime;
        private string leastCash;
        //private string head;
        //private string setDate;
        //private string setTime;

        //属性
        public string TemporatyRate { get; set; }
        public string FixeduserFee { get; set; }
        public string UnitTime { get; set; }
        public string LeastTime { get; set; }
        public string PreTime { get; set; }
        public string LeastCash { get; set; }
        public string Head { get; set; }
        public string SetDate { get; set; }
        public string SetTime { get; set; }


        //创建备忘录,将当前需要保存的信息导入并实例化出一个Memento对象
        public BasicStateMemento CreateMemento()
        {
            return (new BasicStateMemento(TemporatyRate, FixeduserFee, UnitTime, LeastCash, LeastTime, PreTime));
        }

        //恢复备忘录
        public void RecoverState(BasicStateMemento memento)
        {
            temporatyRate = memento.TemporatyRate;
            fixeduserFee = memento.FixeduserFee;
            unitTime = memento.UnitTime;
            leastCash = memento.LeastCash;
            leastTime = memento.LeastTime;
            preTime = memento.PreTime;
        }
    }

备忘录类

 public class BasicStateMemento
    {
        //字段
        private string temporatyRate;
        private string fixeduserFee;
        private string unitTime;
        private string leastTime;
        private string preTime;
        private string leastCash;

        //public BasicStateMemento()
        //{

        //}
        //构造方法将相关数据导入
        public  BasicStateMemento(string TemporatyRate, string FixeduserFee, string UnitTime, string LeastTime, string PreTime, string LeastCash)
        {
            this.temporatyRate = TemporatyRate;
            this.fixeduserFee = FixeduserFee;
            this.unitTime = UnitTime;
            this.leastTime = LeastTime;
            this.preTime = PreTime;
            this.leastCash = LeastCash;
        }


        //需要保存的数据属性,接收值不能修改
        public string TemporatyRate
        {
            get { return temporatyRate; }
        }
        public string FixeduserFee
        {
            get { return fixeduserFee; }
        }
        public string UnitTime
        {
            get { return unitTime; }
        }
        public string LeastTime
        {
            get { return leastTime; }
        }
        public string PreTime
        {
            get { return preTime; }
        }
        public string LeastCash
        {
            get { return leastCash; }
        }

    }

总结

先模仿,再总结,再创造

发布了67 篇原创文章 · 获赞 72 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_42957931/article/details/99241882