static void Main() { Originator originator = new Originator(); originator.State = "On"; // Store internal state Caretaker caretaker = new Caretaker(); caretaker.Memento = originator.CreateMemento(); originator.State = "Off"; originator.SetMemento(caretaker.Memento); }
static void Main(string[] args) { IList<Originator.Momento> savedStates = new List<Originator.Momento>(); var originator = new Originator(); originator.SetState("State1"); originator.SetState("State2"); savedStates.Add(originator.SaveToMomento()); originator.SetState("State3"); savedStates.Add(originator.SaveToMomento()); originator.SetState("State4"); originator.RestoreFromMomento(savedStates[0]); Console.ReadKey(); }
static void Main() { Originator o = new Originator(); o.State = "On"; Caretaker c = new Caretaker(); c.Memento = o.CreateMemento(); o.State = "Off"; o.SetMemento(c.Memento); Console.ReadKey(); }
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) { Caretaker caretaker = new Caretaker(); Originator originator = new Originator(new Laptop("Acer Nitro 5", 15.6, "Intel Core i5")); Console.WriteLine(originator); caretaker.AddMemento(originator.Save()); originator.SetLaptop(new Laptop("Apple MacBook 2020", 17, "Apple M1")); Console.WriteLine(originator); caretaker.AddMemento(originator.Save()); originator.Restore(caretaker.GetMemento(0)); Console.WriteLine(originator); }
static void Main(string[] args) { Originator originator = new Originator(); CareTaker careTaker = new CareTaker(); originator.State = "State #1"; originator.State = "State #2"; careTaker.Add(originator.SaveStateToMemento()); originator.State = "State #3"; careTaker.Add(originator.SaveStateToMemento()); originator.State = "State #4"; Console.WriteLine("Current State: " + originator.State); originator.GetStateFromMemento(careTaker.Get(0)); Console.WriteLine("First saved State: " + originator.State); originator.GetStateFromMemento(careTaker.Get(1)); Console.WriteLine("Second saved State: " + originator.State); 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(); }
static void Main(string[] args) { Originator originator = new Originator(); CareTaker careTaker = new CareTaker(); originator.State = "State1"; originator.State = "State2"; originator.State = "State3"; careTaker.Add(originator.SaveStateToMemento()); originator.State = "State4"; careTaker.Add(originator.SaveStateToMemento()); Console.WriteLine(originator.GetStateFromMemento(careTaker.Get(0))); Console.WriteLine(originator.GetStateFromMemento(careTaker.Get(1))); Console.ReadLine(); }
static void Main(string[] args) { Originator o = new Originator(); o.State = "On"; //Originator初始状态为 “On” o.Show(); Caretaker c = new Caretaker(); c.Memento = o.CreateMemento(); o.State = "Off"; //改变状态属性 o.Show(); o.SetMemento(c.Memento); // 通过备忘录恢复初始状态 o.Show(); Console.Read(); }
static void Main() { IOriginator originator = new Originator("", "Arial", "10"); var restorePoints = new List <IDocumentMemento> { originator.CreateRestorePoint() }; originator.GoToStep1(); restorePoints.Add(originator.CreateRestorePoint()); Console.WriteLine(originator.GetStateAsString()); originator.GoToStep2(); restorePoints.Add(originator.CreateRestorePoint()); Console.WriteLine(originator.GetStateAsString()); originator.Restore(restorePoints.Skip(1).First()); Console.WriteLine(originator.GetStateAsString()); }
static void Main(string[] args) { Originator originator = new Originator(); originator.State = "On"; Console.WriteLine(originator.State); Caretaker caretaker = new Caretaker(); caretaker.Memento = originator.CreateMemento(); originator.State = "Off"; Console.WriteLine(originator.State); originator.SetMemento(caretaker.Memento); Console.WriteLine(originator.State); Console.Read(); }
public static void Main(string[] args) { var originator = new Originator(); var state = "ON"; originator.SetState(state); var caretaker = new Caretaker(); caretaker.SetMemento(originator.CreateMemento()); state = "OFF"; originator.SetState(state); originator.RestoreMemento(caretaker.Memento); Console.ReadKey(); }
public static void Run() { Originator originator = new Originator(); originator.ledTV = new LEDTV("42 inch", "60000Rs", false); Caretaker caretaker = new Caretaker(); caretaker.AddMemento(originator.CreateMemento()); originator.ledTV = new LEDTV("46 inch", "80000Rs", true); caretaker.AddMemento(originator.CreateMemento()); originator.ledTV = new LEDTV("50 inch", "100000Rs", true); Console.WriteLine("\nOrignator current state : " + originator.GetDetails()); Console.WriteLine("\nOriginator restoring to 42 inch LED TV"); originator.ledTV = caretaker.GetMemento(0).ledTV; Console.WriteLine("\nOrignator current state : " + originator.GetDetails()); Console.ReadKey(); }
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 }
//Caretaker static void Main(string[] args) { List <Memento> savedStates = new List <Memento>(); Originator originator = new Originator(); originator.SetState("State1"); savedStates.Add(originator.Create()); originator.SetState("State2"); originator.SetState("State3"); savedStates.Add(originator.Create()); originator.SetState("State4"); foreach (var state in savedStates) { originator.Restore(state); } }
static void Main(string[] args) { Originator ori = new Originator(); ori.State = "On"; ori.Show(); Caretaker c = new Caretaker(); c.Memento = ori.CreateMemento(); ori.State = "Off"; ori.Show(); ori.SetMemento(c.Memento); ori.Show(); Console.ReadKey(); }
static void Main(string[] args) { //Ponto de entrada.. Originator o = new Originator(); o.State = "On"; //Armazenando o estado.. Caretaker c = new Caretaker(); c.Memento = o.CreateMemento(); //Modificando o estado.. o.State = "Off"; //Voltando o estado armazenado.. o.SetMemento(c.Memento); Console.ReadKey(); }
static void Main(string[] args) { 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(); }
static void Main(string[] args) { var originator = new Originator { State = "State A" }; Console.WriteLine(originator.State); var memento = originator.CreateMemento(); var caretaker = new Caretaker { Memento = memento }; originator.State = "State B"; Console.WriteLine(originator.State); originator.SetMemento(caretaker.Memento); Console.WriteLine(originator.State); Console.Read(); }
public static void Main() { Originator originator = new Originator(); CareTaker careTaker = new CareTaker(); originator.ChangeState(new State("状态一")); originator.ChangeState(new State("状态二")); //存储当前状态 string key = originator.GetState().name; careTaker.AddMemento(key, originator.CreateMemento()); originator.ChangeState(new State("状态三")); //恢复到之前备份的某一状态 originator.RestoreFromMemento(careTaker.GetMemento(key)); Debug.Log("目前状态:" + originator.GetState().name); }
static void Main(string[] args) { // Implementation example from GOF book, pg 287 var mementoes = new Stack <Originator.Memento>(); var originator = new Originator(); Console.WriteLine("Initial state:"); originator.Print(); mementoes.Push(originator.CreateMemento()); originator.Update(); Console.WriteLine($"State @ {DateTime.Now}:"); originator.Print(); originator.SetMemento(mementoes.Pop()); Console.WriteLine("Restored initial state:"); originator.Print(); Console.ReadLine(); }
static void Main() { Originator originator = new Originator(); CareTaker careTaker = new CareTaker(); originator.SetState("State #1"); originator.SetState("State #2"); careTaker.Add(originator.SaveStateToMemento()); originator.SetState("State #3"); careTaker.Add(originator.SaveStateToMemento()); originator.SetState("State #4"); Console.WriteLine("Current State: " + originator.GetState()); originator.GetStateFromMemento(careTaker.Get(0)); Console.WriteLine("First saved State: " + originator.GetState()); originator.GetStateFromMemento(careTaker.Get(1)); Console.WriteLine("Second saved State: " + originator.GetState()); }
static void Main(string[] args) { Originator origin = new Originator(); CareTaker taker = new CareTaker(); origin.setState("State #1"); origin.setState("State #2"); taker.add(origin.saveStateToMemento()); origin.setState("State #3"); taker.add(origin.saveStateToMemento()); origin.setState("State #4"); // current state Console.WriteLine("Current state: " + origin.getState()); origin.getStateFromMemento(taker.get(0)); Console.WriteLine("First saved state: " + origin.getState()); origin.getStateFromMemento(taker.get(1)); Console.WriteLine("Second saved state: " + origin.getState()); Console.Read(); }
/// <summary> /// The test second. /// </summary> private static void TestSecond() { Originator originator = new Originator { State = "On" }; // Store internal state Caretaker caretaker = new Caretaker { AnotherMemento = originator.CreateAnotherMemento() }; // Continue changing originator originator.State = "Off"; // Restore saved state originator.SetAnotherMemento(caretaker.AnotherMemento); // Wait for user Console.ReadKey(); }
static void Main(string[] args) { // canonical var originator = new Originator(); originator.State = "ON"; var caretaker = new Caretaker(); caretaker.memento = originator.CreateMemento(); originator.State = "OFF"; originator.SetMemento(caretaker.memento); Console.WriteLine(); // live var paint = new GraphicProcessor(); paint.DrawNewFigure(new Draw { Name = "line1" }); paint.DrawNewFigure(new Draw { Name = "line2" }); paint.DrawNewFigure(new Draw { Name = "triangleA" }); var mem = new SwapMemmory(); mem.memento = paint.CreateMemento(); paint.DrawNewFigure(new Draw { Name = "line5" }); paint.SetMemento(mem.memento); }
public static void Run() { Console.WriteLine("This structural code demonstrates the Memento pattern which temporary saves and restores another object's internal state.\n"); Originator o = new Originator(); o.State = "On"; Caretaker c = new Caretaker(); c.Memento = o.CreateMemento(); o.State = "Off"; o.SetMemento(c.Memento); /* * State = On * State = Off * Restoring state: * State = On */ }
public static void Main(string[] args) { Caretaker careTaker = new Caretaker(); Originator originator = new Originator(); originator.State = "State1"; careTaker.AddMemento(originator.SaveToMemento()); originator.State = "State2"; careTaker.AddMemento(originator.SaveToMemento()); originator.State = "State3"; careTaker.AddMemento(originator.SaveToMemento()); originator.State = "State4"; careTaker.AddMemento(originator.SaveToMemento()); originator.RestoreFromMemento(careTaker.GetMemento(2)); Console.ReadLine(); }
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(); }
static void Main(string[] args) { Originator originator = new Originator("stato 1"); //CareTaker ct = new CareTaker(originator.SaveMemento()); CareTaker ct = new CareTaker(); ct.AddMemento(originator.SaveMemento()); Console.WriteLine("stato: " + originator.Text); originator.Text = "stato modificato"; Console.WriteLine("stato: " + originator.Text); ct.AddMemento(originator.SaveMemento()); originator.Text = "stato modificato2"; Console.WriteLine("stato: " + originator.Text); ct.AddMemento(originator.SaveMemento()); originator.Text = "stato modificato3"; Console.WriteLine("stato: " + originator.Text); originator.RestoreMemento(ct.GetMemento()); Console.WriteLine("stato: " + originator.Text); originator.RestoreMemento(ct.GetMemento()); Console.WriteLine("stato: " + originator.Text); //originator.RestoreMemento(ct.Memento); //Console.WriteLine("stato: " + originator.Text); // The code provided will print ‘Hello World’ to the console. // Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app. Console.WriteLine("Hello World!"); Console.ReadKey(); // Go to http://aka.ms/dotnet-get-started-console to continue learning how to build a console app! }
private static void Main() { var originator = new Originator { State = "ON" }; Console.WriteLine("State is " + originator.State); var caretaker = new Caretaker { Memento = originator.CreateMemento() }; originator.State = "OFF"; Console.WriteLine("State is " + originator.State); Console.WriteLine("Restoring state..."); originator.SetMemento(caretaker.Memento); Console.WriteLine("State is " + originator.State); Console.ReadKey(); }
static void Main(string[] args) { #region 工厂方法 double total = 0.0d; CashContext cc = new CashContext(new CashNormal()); total = cc.GetResult(100.04); cc = new CashContext(new CashRebate("0.8")); total = cc.GetResult(100.04); Console.WriteLine(total); #endregion #region 装饰器方法 Decorator.Component person = new Decorator.Component("xiaocai"); Tshirt tshirt = new Tshirt(); BigTrouser bt = new BigTrouser(); bt.Decorator(person); tshirt.Decorator(bt); tshirt.show(); Console.WriteLine("*****************************"); #endregion #region 代理方法 SchoolGirl sg = new SchoolGirl(); sg.Name = "李娇骄"; Proxy.Proxy daili = new Proxy.Proxy(sg); daili.GiveDolls(); daili.GiveFlowers(); #endregion #region 原型模式 ConcretePrototype1 p1 = new ConcretePrototype1("123"); ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone(); Console.WriteLine("Cloned :"+c1.Id); Resume a = new Resume("Andy"); a.setInfo("Man", "24"); a.setWorkExperience("1998-2005","IBM "); Resume b = (Resume)a.Clone(); b.setWorkExperience("2002-2005", "Dell"); a.display(); b.display(); #endregion #region 模板模式 Console.WriteLine("Student A testPaper:"); TestPaperA testA = new TestPaperA(); testA.Test1(); testA.Test2(); Console.WriteLine("Student B testPaper:"); TestPaperB testB = new TestPaperB(); testB.Test1(); testB.Test2(); #endregion #region 抽象工厂方法 User user = new User(); IFactory factory = new SqlServerFactory(); IUser iu = factory.CreateUser(); //IUser riu = (IUser)Assembly.Load("AbstractFactory").CreateInstance("SqlserverUser"); //反射 //Assembly.Load("程序集名称").CreateInstance("程序集名称.类名称"); iu.Insert(user); iu.GetUser(1); #endregion #region Facade 外观模式 Fund jijin = new Fund(); jijin.BuyFund(); jijin.sellFund(); #endregion #region 建造者模式 Director director = new Director(); abstractBuilder builder1 = new Builder1(); abstractBuilder builder2 = new BuilderB(); director.Construct(builder1); Builder.Builder b1 = builder1.getBuilder(); b1.show(); director.Construct(builder2); Builder.Builder b2 = builder2.getBuilder(); b2.show(); #endregion #region 观察者模式 Observer.ConcreteSubject s = new Observer.ConcreteSubject(); s.Attach(new Observer.ConcreteObserver(s, "x")); s.Attach(new Observer.ConcreteObserver(s, "y")); s.SubjectState = "ABC"; s.Notify(); ///下面是使用委托 ///委托就是一种引用方法的类型。一旦为委托分配了方法,委托将于该方法具有完全相同的行为。 ///委托方法的使用可以像其他的方法一样具有参数和返回值。委托可以看作是对函数的抽象,是函数的”类“,委托的实例将代表一个具体的函数 ///一个委托可以搭载多个方法,所有方法被依次唤起,委托搭载的方法不需要属于同一个类,只需要具有相同的原型和形式,也就是拥有相同的参数列表和返回类型。 ///在使用带参数的委托时,只需要在声明事件的地方将参数传递给事件。在绑定时将调用的方法绑定给事件。 Bosscs boss = new Bosscs(); StockObserver tongshi1 = new StockObserver("tongshi1",boss); NBAObserver tongshiNBA = new NBAObserver("tongshiNBA", boss); boss.Update += new EventHandler1(tongshi1.CloseStockMarket); boss.Update += new EventHandler1(tongshiNBA.CloseStockMarket); boss.update2 += new EventHandler2(tongshiNBA.print); boss.SubjectState = " I am back "; boss.Notify(); #endregion #region 状态模式 State.Context c = new State.Context(new CreateStateA()); c.Request(); c.Request(); c.Request(); c.Request(); #endregion #region 备忘录模式 Originator o = new Originator(); o.State = "On"; o.Show(); Caretaker care = new Caretaker(); care.Memento = o.CreateMemento(); o.State = "Off"; o.Show(); o.SetMemento(care.Memento); o.Show(); GameRole gameRole = new GameRole(); gameRole.GetInitState(); gameRole.StateDisplay(); RoleStateManager stateManager = new RoleStateManager(); stateManager.Memento = gameRole.SaveState(); gameRole.Fight(); gameRole.StateDisplay(); gameRole.RecoveryState(stateManager.Memento); gameRole.StateDisplay(); #endregion #region 组合模式 Composite.Composite root = new Composite.Component("root"); root.Add(new Leaf("Leaf A")); root.Add(new Leaf("Leaf B")); Composite.Composite comp = new Composite.Component("comp X"); comp.Add(new Leaf("Leaf XA")); comp.Add(new Leaf("Leaf XB")); root.Add(comp); Composite.Composite comp2 = new Composite.Component("Comp X2"); comp2.Add(new Leaf("Leaf X2A")); comp2.Add(new Leaf("Leaf X2B")); comp.Add(comp2); root.Add(new Leaf("Leaf C")); Leaf leaf = new Leaf("Leaf D"); root.Add(leaf); root.Display(1); root.Remove(leaf); root.Display(1); #endregion #region 迭代器模式 ConCreteAggregate aggregate = new ConCreteAggregate(); aggregate[0] = "大鸟"; aggregate[1] = "小菜"; aggregate[2]="行李"; aggregate[3] = "老外"; aggregate[4] = "小偷"; Iterator.Iterator myIterator = new ConCreteIterator(aggregate); object item = myIterator.First(); while (!myIterator.IsDone()) { Console.WriteLine(myIterator.CurrentItem() + "请买车票"); myIterator.Next(); } #endregion #region 单例模式 //所有类都有构造方法,不编码则默认生成空的构造方法,若有显示定义的构造方法,默认的构造方法就会失效。只要将构造方法改写为私有的,外部的程序就不能通过new 来初始化它。 //通过一个共有的方法来返回类的实例。 Singleton.Singleton s1 = Singleton.Singleton.GetInstance(); Singleton.Singleton s2 = Singleton.Singleton.GetInstance(); if (s1 == s2) { Console.WriteLine("两个对象是相同的实例。"); } #endregion #region 命令模式 Receiver r = new Receiver(); Command.Command command = new Command.ConcreteCommand(r); Invoker invoker = new Invoker(); invoker.SetCommand(command); invoker.ExecuteCommand(); #endregion #region 职责链模式 Handler h1 = new ConcreteHandler1(); Handler h2 = new ConcreteHandler2(); h1.SetSuccessor(h2); int[] requests = { 2, 3, 4, 5, 6, 12, 34, 11, 15 }; foreach (int request in requests) { h1.HandlerRequest(request); } #endregion #region 中介者模式 ConcreteMediator mediator = new ConcreteMediator(); ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator); ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator); mediator.Colleague1 = colleague1; mediator.Colleague2 = colleague2; colleague1.Send("吃饭了吗?"); colleague2.Send("还没有呢"); #endregion #region 享元模式 int extri = 22; FlyweightFactory f = new FlyweightFactory(); Flyweight.Flyweight fx = f.GetFlyweight("X"); fx.Operation(--extri); Flyweight.Flyweight fy = f.GetFlyweight("Y"); fy.Operation(--extri); Flyweight.Flyweight fz = f.GetFlyweight("Z"); fz.Operation(--extri); #endregion #region 解释器模式 <<<<<<< HEAD Interpreter.Context context = new Interpreter.Context(); IList<Interpreter.AbstractExpression> list = new List<Interpreter.AbstractExpression>(); list.Add(new Interpreter.TerminalExpression()); list.Add(new Interpreter.NormalExpression()); foreach (Interpreter.AbstractExpression exp in list) exp.Interpret(context); ======= Interpreter.Context context1 = new Interpreter.Context(); IList<AbstractExpression> list = new List<AbstractExpression>(); list.Add(new TerminalExpression()); list.Add(new NonTerminalExpression()); foreach (AbstractExpression exp in list) { exp.Interpreter(context1); } #endregion #region 访问者模式 ObjectStructure os = new ObjectStructure(); os.Add(new Man()); os.Add(new Woman()); Success v1 = new Success(); os.Display(v1); Failing f1 = new Failing(); os.Display(f1); Amativeness a1 = new Amativeness(); os.Display(a1); >>>>>>> 77e342ef6e96917a8dc01e72e41626dcffd4ba13 #endregion Console.Read(); }
//白箱备忘录 //暴露了备忘录中的状态给任何对象 //public State GetState() //{ // return state; //} //黑箱备忘录 //提供一个方法,反调“以参数形式传入的Originator”的改变状态接口(或者将originator直接放在Memento构造时)。 //这样, 虽然这个方法是公有的,但只可能被Originator使用, 保证了备忘录的安全性。缺点是, Memento反向依赖了Originator,增加了耦合度。 public void Restore(Originator originator) { originator.ChangeState(this.state); }
static void Main() { var originator = new Originator {State = "State A"}; Console.WriteLine(originator.State); var caretaker = new Caretaker {Memento = originator.CreateMemento()}; originator.State = "State B"; Console.WriteLine(originator.State); originator.SetMemento(caretaker.Memento); Console.WriteLine(originator.State); }
public static void RestoreState(Originator <T> orig, int checkpoint) { orig.RestoreMemento(mementoList[checkpoint]); }
public Caretaker(Originator <T> originator) { Originator = originator; }
public Caretaker(Originator originator) { this._originator = originator; }
public static void SaveState(Originator <T> orig) { mementoList.Add(orig.CreateMemento()); }