예제 #1
0
        /// <summary>
        /// In the Memento pattern, we need to capture and externalize an object's state so that the object can be
        /// restored to that state at a later time.  A good example of this is undo/redo operations.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //Here's a new supplier for our restaurant
            FoodSupplier s = new FoodSupplier();

            s.Name  = "Harold Karstark";
            s.Phone = "(482) 449-1172";

            // Let's store that entry in our database.
            SupplierMemory m = new SupplierMemory();

            m.Memento = s.SaveMemento();

            // Continue changing originator
            s.Address = "548 S Main St. Nowhere, KS";

            // Crap, gotta undo that entry, I entered the wrong address
            s.RestoreMemento(m.Memento);

            Console.ReadKey();
        }
예제 #2
0
        /// <summary>
        /// Cancel the action, the command will return the object to its previous state.
        /// </summary>
        public void Undo()
        {
            if (_mementos.Count == 0)
            {
                return;
            }

            IMemento memento = _mementos.Last();

            _mementos.Remove(memento);

            Console.WriteLine($"\nSupplierMemory: Restoring state to: {memento.GetName()}");

            try
            {
                _foodSupplier.RestoreMemento(memento);
            }
            catch (Exception)
            {
                Undo();
            }
        }