首页 > 其他分享 >CSharp: Memento Pattern in donet core 6

CSharp: Memento Pattern in donet core 6

时间:2022-12-19 23:13:01浏览次数:41  
标签:donet string core Pattern 供应商 System using public Memento

 

using Geovin.Du.DuMemento.Conceptual;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Geovin.Du.DuMemento.FoodSupplier
{

    /// <summary>
    /// 备忘录模式 Memento Pattern亦称: 快照、Snapshot、Memento
    /// </summary>
    public class FoodSupplier
    {

        /// <summary>
        /// 
        /// </summary>
        private string _name = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        private string _phone = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        private string _address = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        public string Name
        {
            get => _name;
            set
            {
                _name = value;
                Console.WriteLine($"食品供应商:名称设置为 {_name}");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Phone
        {
            get => _phone;
            set
            {
                _phone = value;
                Console.WriteLine($"食品供应商:电话号码设置为 {_phone}");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Address
        {
            get => _address;
            set
            {
                _address = value;
                Console.WriteLine($"食品供应商:地址设置为{_address}");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return $"姓名:{Name} 电话:{Phone} 地址:{ Address}";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IMemento Save()
        {
            Console.WriteLine("\n食品供应商:保存现状...");
            return new FoodSupplierMemento(_name, _phone, _address);
        }

        /// <summary>
        /// Restore previous state.
        /// Note that the method returns an IMemento instance in order to to facilitate redo operation.
        /// The return type can be changed to void if redo doesn't have to be supported.
        /// </summary>
        /// <param name="memento">Memento for restoration.</param>
        /// <returns>Redo memento.</returns>
        public IMemento Restore(IMemento memento)
        {
            Console.WriteLine("\n食品供应商:恢复状态...");

            if (memento is not FoodSupplierMemento foodSupplierMemento)
            {
                throw new ArgumentException($"未知的纪念品: {memento}");
            }

            var redoMemento = new FoodSupplierMemento(_name, _phone, _address);

            Name = foodSupplierMemento.Name;
            Phone = foodSupplierMemento.PhoneNumber;
            Address = foodSupplierMemento.Address;

            return redoMemento;
        }
    }

}


using Geovin.Du.DuMemento.Conceptual;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Geovin.Du.DuMemento.FoodSupplier
{

    /// <summary>
    /// 备忘录模式 Memento Pattern亦称: 快照、Snapshot、Memento
    /// </summary>
    public class SupplierRegistry
    {

        /// <summary>
        /// 
        /// </summary>
        private readonly FoodSupplier _supplier;
        /// <summary>
        /// 
        /// </summary>
        private readonly Stack<IMemento> _undoStack;
        /// <summary>
        /// 
        /// </summary>
        private readonly Stack<IMemento> _redoStack;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="supplier"></param>
        public SupplierRegistry(FoodSupplier supplier)
        {
            _supplier = supplier;
            _undoStack = new Stack<IMemento>();
            _redoStack = new Stack<IMemento>();
        }
        /// <summary>
        /// 
        /// </summary>
        public void Backup()
        {
            Console.WriteLine("\n供应商注册表:备份操作..");

            _redoStack.Clear();
            _undoStack.Push(_supplier.Save());
        }
        /// <summary>
        /// 
        /// </summary>
        public void Undo()
        {
            Console.WriteLine("\n供应商注册表:正在执行撤销操作...");

            var top = _undoStack.Pop();
            _redoStack.Push(_supplier.Restore(top));
        }
        /// <summary>
        /// 
        /// </summary>
        public void Redo()
        {
            Console.WriteLine("\n供应商注册表:执行重做操作...");

            var top = _redoStack.Pop();
            _undoStack.Push(_supplier.Restore(top));
        }
    }
}


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

namespace Geovin.Du.DuMemento.FoodSupplier
{

    /// <summary>
    /// 备忘录模式 Memento Pattern亦称: 快照、Snapshot、Memento
    /// </summary>
    public interface IMemento
    {
    }
}


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

namespace Geovin.Du.DuMemento.FoodSupplier
{

    /// <summary>
    /// 备忘录模式 Memento Pattern亦称: 快照、Snapshot、Memento
    /// </summary>
    public class FoodSupplierMemento : IMemento
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="address"></param>
        public FoodSupplierMemento(string name, string phoneNumber, string address)
        {
            Name = name;
            PhoneNumber = phoneNumber;
            Address = address;
        }
        /// <summary>
        /// 
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// /
        /// </summary>
        public string PhoneNumber { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Address { get; set; }
    }

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Geovin.Du.BuildingBlocks;


namespace Geovin.Du.DuMemento.FoodSupplier
{

    /// <summary>
    /// 备忘录模式 Memento Pattern亦称: 快照、Snapshot、Memento
    /// </summary>
    public static class FoodSupplierExecutor
    {


        /// <summary>
        /// 
        /// </summary>
        public static void Execute()
        {
            ConsoleExtension.WriteSeparator("\n备忘录模式 Memento Pattern 食物供应 Food supplier demo");

            var foodSupplier = new FoodSupplier
            {
                Name = "涂氏食品工业公司",
                Phone = "8888 1111",
                Address = "深圳市罗湖区",
            };

            var registry = new SupplierRegistry(foodSupplier);
            registry.Backup();

            foodSupplier.Phone = "8888 2222";

            registry.Undo();
            registry.Redo();
        }
    }
}

  

 

调用:

Geovin.Du.DuMemento.FoodSupplier.FoodSupplierExecutor.Execute();

  

输出:

备忘录模式 Memento Pattern 食物供应 Food supplier demo
--------------------------------------------------
食品供应商:名称设置为 涂氏食品工业公司
食品供应商:电话号码设置为 8888 1111
食品供应商:地址设置为深圳市罗湖区

供应商注册表:备份操作..

食品供应商:保存现状...
食品供应商:电话号码设置为 8888 2222

供应商注册表:正在执行撤销操作...

食品供应商:恢复状态...
食品供应商:名称设置为 涂氏食品工业公司
食品供应商:电话号码设置为 8888 1111
食品供应商:地址设置为深圳市罗湖区

供应商注册表:执行重做操作...

食品供应商:恢复状态...
食品供应商:名称设置为 涂氏食品工业公司
食品供应商:电话号码设置为 8888 2222
食品供应商:地址设置为深圳市罗湖区

  

标签:donet,string,core,Pattern,供应商,System,using,public,Memento
From: https://www.cnblogs.com/geovindu/p/16993341.html

相关文章