示例#1
0
        private static void Main()
        {
            //Here's a new supplier for our restaurant.
            FoodSupplier supplier = new FoodSupplier("Harold Karstark", "(482) 555-1172", "S Main St. Nowhere, KS");

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

            memory.Backup();
            supplier.Name = "A";

            memory.Backup();
            supplier.Name = "B";

            memory.Backup();
            supplier.Name = "C";

            Console.WriteLine();
            memory.ShowHistory();

            Console.WriteLine("\nClient: Now, let's rollback!\n");
            memory.Undo();

            Console.WriteLine("\n\nClient: Once more!\n");
            memory.Undo();

            Console.WriteLine("\n\nClient: Once more!\n");
            memory.Undo();
        }
示例#2
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();
        }