Patterns in SOME –Memento

    技术2022-05-11  98

    Code in C#:   namespace Memento_DesignPattern {      using System;        class Originator      {          private double manufacturer=0;          private double distributor = 0;          private double retailer = 0;             public void MakeSale(double purchasePrice)          {               // We assume sales are divided equally amount the three               manufacturer += purchasePrice * .40;               distributor += purchasePrice *.3;               retailer += purchasePrice *.3;               // Note: to avoid rounding errors for real money handling               // apps, we should be using decimal integers               // (but hey, this is just a demo!)          }            public Memento CreateMemento()          {               return (new Memento(manufacturer, distributor, retailer));                     }                   public void SetMemento(Memento m)          {               manufacturer = m.A;               distributor = m.B;               retailer = m.C;          }             }        class Memento      {          private double iA;          private double iB;          private double iC;            public Memento(double a, double b, double c)          {               iA = a;               iB = b;               iC = c;          }            public double A          {               get               {                    return iA;               }          }            public double B          {               get               {                    return iB;               }          }            public double C          {               get               {                    return iC;               }          }      }        class caretaker      {               }        ///<summary>      ///    Summary description for Client.      ///</summary>      public class Client      {          public static int Main(string[] args)          {                          Originator o = new Originator();                             // Assume that during the course of running an application               // we we set various data in the originator               o.MakeSale(45.0);               o.MakeSale(60.0);                 // Now we wish to record the state of the object               Memento m = o.CreateMemento();                 // We make further changes to the object               o.MakeSale(60.0);               o.MakeSale(10.0);               o.MakeSale(320.0);                 // Then we decide ot change our minds, and revert to the saved state (and lose the changes since then)               o.SetMemento(m);                 return 0;          }      } }     Code in SOME:    COriginator                   double _manufacturer        double _distributor        double _retailer        MakeSale(double)        CMemento CreateMemento()        SetMemento(CMemento)   CMemento        (double r_A,double r_B,double r_C)            //a constructor and 3 read-only properties   Ccaretaker     CClient        main   CClient.main {        COriginator o.();        o.MakeSale(purchasePrice[45.])        {               <%                                           _manufacturer += purchasePrice * 0.40;                      _distributor += purchasePrice * 0.3;                      _retailer += purchasePrice * 0.3;              %>        };        o.MakeSale(60.);               CMemento m = o.CreateMemento()                                                             /*creation and assign*/        {               CMemento.( _a = _manufacturer, _b = _distributor, _c = _retailer);                        /* anonymous creation means it will be returned */        };               o.MakeSale(60.);        o.MakeSale(10.);        o.MakeSale(320.);               o.SetMemento(m)                {               _manufacturer = m.A;               _distributor = m.B;               _retailer = m.C;        }; }

    最新回复(0)