Patterns in SOME –Memento

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;
       };
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值