public static void Main(string[] args) { Console.WriteLine("========備忘錄模式-測試========"); Console.WriteLine(""); GameRole boss = new GameRole(); boss.display(); Console.WriteLine("使用複雜的密技降低Boss攻擊力"); boss.ATK = 60; //趕快存檔 RoleStateCateTaker rsc = new RoleStateCateTaker(); rsc.setSave(boss.save()); boss.display(); //開打 boss.fight(); boss.display(); //隊伍太廢,倒了一半,快速讀取剛才的存檔 boss.load(rsc.getSave()); Console.WriteLine("重新讀檔!"); boss.display(); }
static void Main(string[] args) {//儲存物件的重要狀態 //維護物件的封裝 Console.WriteLine("大戰boss前 初始狀態"); GameRole role = new GameRole(); role.GetInitState(); role.StateDisplay(); Console.WriteLine("保存進度"); RoleStateTaker stateAdmin = new RoleStateTaker(); stateAdmin.Memento = role.SaveState(); //不知道封裝那些資料 Console.WriteLine("開始Fight!!"); role.Fight(); role.StateDisplay(); Console.WriteLine("恢復原始狀態"); role.RecoveryState(stateAdmin.Memento); role.StateDisplay(); Console.ReadKey(); }
static void Main(string[] args) { //大战Boss前 GameRole lixiaoyao = new GameRole(); lixiaoyao.GetInitState(); lixiaoyao.StateDisplay(); //保存进度 RoleStateCaretaker stateAdmin = new RoleStateCaretaker(); stateAdmin.Memento = lixiaoyao.SaveState(); //大战Boss时,损耗严重 lixiaoyao.Fight(); lixiaoyao.StateDisplay(); //恢复之前状态 lixiaoyao.RecoveryState(stateAdmin.Memento); lixiaoyao.StateDisplay(); Console.Read(); }
static void Main(string[] args) { GameRole lixiaoyao = new GameRole(); lixiaoyao.GetInitState(); lixiaoyao.StateDisplay(); RoleStateCaretaker stateCaretaker = new RoleStateCaretaker {RoleStateMemento = lixiaoyao.SaveState()}; lixiaoyao.Fight(); lixiaoyao.StateDisplay(); lixiaoyao.RevcoveryState(stateCaretaker.RoleStateMemento); lixiaoyao.StateDisplay(); Console.Read(); }
static void Main(string[] args) { GameRole ben = new GameRole(); ben.SetInitState(); ben.StateDisplay(); RoleStateCareTaker stateAdmin = new RoleStateCareTaker(); stateAdmin.memento = ben.SaveState(); ben.Fight(); ben.StateDisplay(); ben.RecoveryState(stateAdmin.memento); ben.StateDisplay(); }
static void Main(string[] args) { // 打王前 GameRole character = new GameRole(); character.GetInitState(); character.StateDisplay(); // 保存進度 RoleStateCaretaker stateAdmin = new RoleStateCaretaker(); stateAdmin.Memento = character.SaveState(); // 打王 character.Fight(); character.StateDisplay(); // 讀取檔案 character.RecoveryState(stateAdmin.Memento); character.StateDisplay(); Console.WriteLine(); Originator originator = new Originator(); // 狀態初始屬性為On originator.State = "On"; originator.Show(); CareTaker careTaker = new CareTaker(); // 保存狀態時,由於有了很好的封裝,可以隱藏Originator的實現細節 careTaker.Memento = originator.CreateMemento(); // 狀態屬性改為Off originator.State = "Off"; originator.Show(); // 恢復原初始狀態 originator.SetMemento(careTaker.Memento); originator.Show(); Console.ReadLine(); }
static void Main(string[] args) { GameRole lixiaoyao = new GameRole(); lixiaoyao.GetInitState(); lixiaoyao.StateDisplay(); RoleStateCaretaker stateCaretaker = new RoleStateCaretaker(); stateCaretaker.Memento = lixiaoyao.SaveState(); lixiaoyao.Fight(); lixiaoyao.StateDisplay(); lixiaoyao.RecoveryState(stateCaretaker.Memento); lixiaoyao.StateDisplay(); Console.Read(); }
static void Main(string[] args) { GameRole gr = new GameRole(); gr.InitState(); gr.StateDisplay(); RoleStateCaretaker rsc = new RoleStateCaretaker(); rsc.RoleMemento = gr.SaveState(); gr.Fight(); gr.StateDisplay(); gr.RecoveryState(rsc.RoleMemento); gr.StateDisplay(); Console.ReadKey(); }
private static void Main(string[] args) { GameRole solider = new GameRole(); solider.GetInitState(); solider.StateDisplay(); RoleStateCaretaker stateAdmin = new RoleStateCaretaker(); stateAdmin.Memento = solider.SaveState(); solider.Fight(); solider.StateDisplay(); solider.RecoveryState(stateAdmin.Memento); solider.StateDisplay(); Console.Read(); }
static void Main(string[] args) { #region 基本用法(Basic.cs) Originator o = new Originator(); //Originator初始状态,状态属性为“On” o.State = "On"; o.Show(); Caretaker c = new Caretaker(); //保存状态时,由于有了很好的封装,可以隐藏Originator的实现细节 c.Memento = o.CreateMemento(); //Originator改变了状态属性为“Off” o.State = "Off"; o.Show(); //恢复原初始状态 o.SetMemento(c.Memento); o.Show(); #endregion #region 具体实例(Example.cs) //大战Boss前 GameRole lixiaoyao = new GameRole(); lixiaoyao.GetInitState(); lixiaoyao.StateDisplay(); //保存进度 RoleStateCaretaker stateAdmin = new RoleStateCaretaker(); stateAdmin.Memento = lixiaoyao.SaveState(); //大战Boss时,损耗严重 lixiaoyao.Fight(); lixiaoyao.StateDisplay(); //恢复之前的状态 lixiaoyao.RecoveryState(stateAdmin.Memento); lixiaoyao.StateDisplay(); Console.Read(); #endregion }
static void Main(string[] args) { //备忘录 Originator o = new Originator(); o.State = "On"; o.Show(); Caretaker c = new Caretaker(); c.Memento = o.CreateMemento(); o.State = "Off"; o.Show(); o.SetMement(c.Memento); o.Show(); GameRole gr = new GameRole(); gr.InitState(); gr.DisplayInfo(); RoleCaretaker roletaker = new RoleCaretaker(); roletaker.Rolememento = gr.CreateRoleMemento(); gr.Over(); gr.DisplayInfo(); gr.RecoveryRoleMemento(roletaker.Rolememento); gr.DisplayInfo(); Console.Read(); }