您的位置:首页 > 编程语言 > C#

201802262211->深入浅出设计模式:c#备忘录模式

2018-02-26 22:11 603 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace _017备忘录模式

{

    /// <summary>

    /// 样本

    /// </summary>

    public class Originate

    {

        public string State;

        /// <summary>

        /// 备份

        /// </summary>

        /// <returns></returns>

        public Memento CareTaker()

        {

            return new Memento(State);

        }

    }

    /// <summary>

    /// 备份处

    /// </summary>

    public class Memento

    {

        private string State;

        public Memento(string state)

        {

            this.State = state;

        }

        public string getState()

        {

            return this.State;

        }

    }

    internal class Program

    {

        private static void Main(string[] args)

        {

            Originate orginate = new Originate();

            orginate.State = "on";

            Console.WriteLine(orginate.State);

            Memento memento = orginate.CareTaker();

            orginate.State = "off";

            Console.WriteLine(orginate.State);

            orginate.State = memento.getState();

            Console.WriteLine(orginate.State);

            /*

             * 备忘录模式

             *

             * 其实就是将一个对象再拷贝一次,以达到备份的目的

             *

             * 但是备份的对象可以拓展,这样能提高开发效率

             *

             * 第一次接触这模式,感觉有点直接和有点奇妙

             *

             */

            Console.ReadKey();

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 心得 理解 笔记