设计模式——备忘模式

名称Memento
结构 Memento.gif
意图在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。
适用性
  • 必须保存一个对象在某一个时刻的(部分)状态, 这样以后需要时它才能恢复到先前的状态。
  • 如果一个用接口来让其它对象直接得到这些状态,将会暴露对象的实现细节并破坏对象的封装性。
Code Example 
  1 None.gif //  Memento
  2 None.gif
  3 None.gif //  Intent: "Without violating encapsulation, capture and externalize an 
  4 None.gif //  object's internal state so that an object can be restored to this 
  5 None.gif //  state later." 
  6 None.gif
  7 None.gif //  For further information, read "Design Patterns", p283, Gamma et al.,
  8 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
  9 None.gif
 10 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
 11InBlock.gif * We often have client code that wishes to record the current state of an 
 12InBlock.gif * object, without being interested in the actual data values (this is 
 13InBlock.gif * needed for undo and checkpointing). To support this behavior, we can have
 14InBlock.gif * the object record its internal data in a helper class called a memento, and
 15InBlock.gif * the client code can treat this as an opaque store of the object's state. 
 16InBlock.gif * At some later point, the client can pass the memento back into the object, 
 17InBlock.gif * to restore it to the previous state. 
 18ExpandedBlockEnd.gif */

 19 None.gif 
 20 None.gif namespace  Memento_DesignPattern
 21 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 22InBlock.gif    using System;
 23InBlock.gif
 24InBlock.gif    class Originator 
 25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 26InBlock.gif        private double manufacturer=0;
 27InBlock.gif        private double distributor = 0;
 28InBlock.gif        private double retailer = 0;
 29InBlock.gif
 30InBlock.gif        public void MakeSale(double purchasePrice)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 32InBlock.gif            // We assume sales are divided equally amount the three
 33InBlock.gif            manufacturer += purchasePrice * .40;
 34InBlock.gif            distributor += purchasePrice *.3;
 35InBlock.gif            retailer += purchasePrice *.3;
 36InBlock.gif            // Note: to avoid rounding errors for real money handling 
 37InBlock.gif            // apps, we should be using decimal integers
 38InBlock.gif            // (but hey, this is just a demo!)
 39ExpandedSubBlockEnd.gif        }

 40InBlock.gif
 41InBlock.gif        public Memento CreateMemento()
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 43InBlock.gif            return (new Memento(manufacturer, distributor, retailer));            
 44ExpandedSubBlockEnd.gif        }

 45InBlock.gif        
 46InBlock.gif        public void SetMemento(Memento m)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 48InBlock.gif            manufacturer = m.A;
 49InBlock.gif            distributor = m.B;
 50InBlock.gif            retailer = m.C;
 51ExpandedSubBlockEnd.gif        }
        
 52ExpandedSubBlockEnd.gif    }

 53InBlock.gif
 54InBlock.gif    class Memento 
 55ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 56InBlock.gif        private double iA;
 57InBlock.gif        private double iB;
 58InBlock.gif        private double iC;
 59InBlock.gif
 60InBlock.gif        public Memento(double a, double b, double c)
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62InBlock.gif            iA = a;
 63InBlock.gif            iB = b;
 64InBlock.gif            iC = c;
 65ExpandedSubBlockEnd.gif        }

 66InBlock.gif
 67InBlock.gif        public double A 
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 69InBlock.gif            get 
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 71InBlock.gif                return iA;
 72ExpandedSubBlockEnd.gif            }

 73ExpandedSubBlockEnd.gif        }

 74InBlock.gif
 75InBlock.gif        public double B 
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 77InBlock.gif            get 
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                return iB;
 80ExpandedSubBlockEnd.gif            }

 81ExpandedSubBlockEnd.gif        }

 82InBlock.gif
 83InBlock.gif        public double C 
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 85InBlock.gif            get 
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 87InBlock.gif                return iC;
 88ExpandedSubBlockEnd.gif            }

 89ExpandedSubBlockEnd.gif        }

 90ExpandedSubBlockEnd.gif    }

 91InBlock.gif
 92InBlock.gif    class caretaker 
 93ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 94InBlock.gif        
 95ExpandedSubBlockEnd.gif    }

 96InBlock.gif
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 98InBlock.gif    ///    Summary description for Client.
 99ExpandedSubBlockEnd.gif    /// </summary>

100InBlock.gif    public class Client
101ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
102InBlock.gif        public static int Main(string[] args)
103ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{              
104InBlock.gif            Originator o = new Originator();
105InBlock.gif            
106InBlock.gif            // Assume that during the course of running an application 
107InBlock.gif            // we we set various data in the originator
108InBlock.gif            o.MakeSale(45.0);
109InBlock.gif            o.MakeSale(60.0);
110InBlock.gif
111InBlock.gif            // Now we wish to record the state of the object
112InBlock.gif            Memento m = o.CreateMemento();
113InBlock.gif
114InBlock.gif            // We make further changes to the object
115InBlock.gif            o.MakeSale(60.0);
116InBlock.gif            o.MakeSale(10.0);
117InBlock.gif            o.MakeSale(320.0);
118InBlock.gif
119InBlock.gif            // Then we decide ot change our minds, and revert to the saved state (and lose the changes since then)
120InBlock.gif            o.SetMemento(m);
121InBlock.gif
122InBlock.gif            return 0;
123ExpandedSubBlockEnd.gif        }

124ExpandedSubBlockEnd.gif    }

125ExpandedBlockEnd.gif}

126 None.gif
127 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/08/10/211291.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值