Esempio n. 1
0
        static void Main(string[] args)
        {
            String     gameName   = "Super mario";
            Caretaker  caretaker  = new Caretaker();
            Originator originator = new Originator();

            Game game = new Game();

            game.setName(gameName);
            game.setCheckpoint(1);
            originator.setState(game);
            caretaker.addMemento(originator.save());
            Console.WriteLine("-----------State point one----------------");
            Console.WriteLine(game.toString());


            game = new Game();
            game.setName(gameName);
            game.setCheckpoint(2);
            originator.setState(game);
            caretaker.addMemento(originator.save());
            Console.WriteLine("-----------State point two----------------");
            Console.WriteLine(game.toString());

            game = new Game();
            game.setName(gameName);
            game.setCheckpoint(3);
            originator.setState(game);

            caretaker.addMemento(originator.save());
            Console.WriteLine("-----------State point three----------------");
            Console.WriteLine(game.toString());

            game = new Game();
            game.setName(gameName);
            game.setCheckpoint(4);

            originator.setState(game);
            caretaker.addMemento(originator.save());
            Console.WriteLine("-----------State point four----------------");
            Console.WriteLine(game.toString());

            game = new Game();
            game.setName(gameName);
            game.setCheckpoint(5);
            originator.setState(game);
            caretaker.addMemento(originator.save());
            Console.WriteLine("-----------State point five----------------");
            Console.WriteLine(game.toString());

            originator.setState(game);
            originator.restore(caretaker.getMemento(0));
            Console.WriteLine("-----------ohhhhhhh you lost----------------");
            Console.WriteLine("-----------Your new state is----------------");
            game = originator.getState();
            Console.WriteLine(game.toString());

            Console.WriteLine(game);
        }
Esempio n. 2
0
        public static void RunTest()
        {
            //Create an originator which creates and gets mementos
            Originator o = new Originator();

            //Set state to On for the originator
            o.state = "On";

            Console.WriteLine("Current state: " + o.state);

            Caretaker c = new Caretaker();

            c.addMemento(o.createMemento());

            o.state = "Off";

            Console.WriteLine("Current state: " + o.state);

            //Stores the snapshot
            o.setMemento(c.getMemento(0));

            Console.WriteLine("Current state: " + o.state);
        }