Exemplo n.º 1
0
        public void Test()
        {
            var originator = new Originator("field 2", 42)
            {
                Property1 = "property 1",
                Field1 = "field 1"
            };

            Assert.That(originator.Field1, Is.EqualTo("field 1"));
            Assert.That(originator.GetField2(), Is.EqualTo("field 2"));
            Assert.That(originator.Property1, Is.EqualTo("property 1"));
            Assert.That(originator.Property2, Is.EqualTo(42));

            var memento = new Memento<Originator>(originator);

            originator.Field1 = "field 1a";
            originator.SetField2("field 2a");
            originator.Property1 = "property 1a";
            originator.SetProperty2(52);

            Assert.That(originator.Field1, Is.EqualTo("field 1a"));
            Assert.That(originator.GetField2(), Is.EqualTo("field 2a"));
            Assert.That(originator.Property1, Is.EqualTo("property 1a"));
            Assert.That(originator.Property2, Is.EqualTo(52));

            memento.Restore(originator);

            Assert.That(originator.Field1, Is.EqualTo("field 1"));
            Assert.That(originator.GetField2(), Is.EqualTo("field 2"));
            Assert.That(originator.Property1, Is.EqualTo("property 1"));
            Assert.That(originator.Property2, Is.EqualTo(42));
        }
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        static void Main()
        {
            Originator o = new Originator();
            o.State = "On";

            // Store internal state
            Caretaker c = new Caretaker();
            c.Memento = o.CreateMemento();

            // Continue changing originator
            o.State = "Off";

            // Restore saved state
            o.SetMemento(c.Memento);

            // Wait for user
            Console.ReadKey();
        }
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        static void Main()
        {
            Originator o = new Originator();

            o.State = "On";

            // Store internal state
            Caretaker c = new Caretaker();

            c.Memento = o.CreateMemento();

            // Continue changing originator
            o.State = "Off";

            // Restore saved state
            o.SetMemento(c.Memento);

            // Wait for user
            Console.ReadKey();
        }
Exemplo n.º 4
0
        public static void Mein()
        {
            var originator = new Originator("Hello world!");
            var caretaker  = new Caretaker(originator);

            caretaker.Backup();
            originator.DoSomething();

            caretaker.Backup();
            originator.DoSomething();

            caretaker.Backup();
            originator.DoSomething();

            Console.WriteLine();
            caretaker.ShowHistory();

            // Undo the changes.
            caretaker.Undo();
            caretaker.ShowHistory();

            caretaker.Undo();
            caretaker.Undo();
        }
Exemplo n.º 5
0
 public Caretaker(Originator originator) => _originator = originator;
Exemplo n.º 6
0
 public Caretaker(Originator originator)
 {
     this._originator = originator;
 }