コード例 #1
0
    private void Start()
    {
        //Originator originator = new Originator();

        //originator.SetState("State1");
        //originator.ShowState();

        //Memento memento = originator.CreatMemento();//创建快照

        //originator.SetState("State2");
        //originator.ShowState();

        //originator.SetMemento(memento);
        //originator.ShowState();

        CareTaker careTaker = new CareTaker();    //多备忘录保存管理器

        Originator originator = new Originator(); //发起者(内部有需要保存的类成员)

        originator.SetState("State1");            //状态1
        originator.ShowState();
        careTaker.AddMemento("v1.0", originator.CreatMemento());

        originator.SetState("State2");//状态2
        originator.ShowState();
        careTaker.AddMemento("v2.0", originator.CreatMemento());

        originator.SetState("State3");//状态3
        originator.ShowState();
        careTaker.AddMemento("v3.0", originator.CreatMemento());

        originator.SetMemento(careTaker.GetMemento("v2.0"));//回到版本2
        originator.ShowState();

        originator.SetMemento(careTaker.GetMemento("v1.0"));//回到版本1
        originator.ShowState();
    }