zoukankan      html  css  js  c++  java
  • 4.6 Memento(备忘录)

    【返回目录】

    大家一定都看过很多关于特工的电影,尽管不算太真实,比较戏剧性,但是我觉得也可以借鉴这些电影中一些常见的场景来解释什么是备忘录模式。

    一天夜里,春暖花开、万物复苏、狗熊撒欢,这是个偷窃的好季节。一个身穿黑色紧身衣武林高手脸上围着黑巾,飞檐走壁,手提一把柳叶刀,迅速来到扬州城王员外的书房屋顶……(糟糕,不好意思,放错碟了,赶紧换),只见这位高手拿出一根细长的绳索,一头固定在屋顶通风口旁的水泥垛上,一头套在自己的腰上,然后他就从通风口下去,不多一会儿他就下到了正对通风口的那个保险柜上方,而这个保险柜里装着的,正是那块绿得让人疯狂的石头。高手今晚的任务就是要用一块假的宝石偷换这块真货。

    为了保持现场不轻易被人看出破绽,高手换宝石的每一步操作都必须要能够还原成原貌才能为自己赢取更多的远走高飞的时间。这是他从身后的工具包中掏出一个爱国者数码相机(抵制日货),他要在做每一步操作之前都要对保险柜进行拍照,以便还原。

       1: using System;
       2:  
       3: namespace Autumoon.DesignPatterns.Memento
       4: {
       5:     public class Original
       6:     {
       7:         public int StatusA { get; set; }
       8:         public int StatusB { get; set; }
       9:         public int StatusC { get; set; }
      10:  
      11:         public void Offset(int offset)
      12:         {
      13:             this.StatusA += offset;
      14:             this.StatusB += offset;
      15:             this.StatusC += offset;
      16:         }
      17:  
      18:         public MementoData CreateMemento()
      19:         {
      20:             return (new MementoData(this.StatusA, this.StatusB, this.StatusC));
      21:         }
      22:  
      23:         public void Resume(MementoData memento)
      24:         {
      25:             this.StatusA = memento.StatusA;
      26:             this.StatusB = memento.StatusB;
      27:             this.StatusC = memento.StatusC;
      28:         }
      29:     }
      30:  
      31:     public class MementoData
      32:     {
      33:         public int StatusA { get; private set; }
      34:         public int StatusB { get; private set; }
      35:         public int StatusC { get; private set; }
      36:  
      37:         public MementoData(int statusA, int statusB, int statusC)
      38:         {
      39:             this.StatusA = statusA;
      40:             this.StatusB = statusB;
      41:             this.StatusC = statusC;
      42:         }
      43:     }
      44: }

    高手就是高手,他不仅成功地拿到了宝石,还最终把犯罪现场还原如初了,但是至于他到底逃脱了没有,欲知后事如何,下回也不分解。

       1: static void Main(string[] args)
       2: {
       3:     #region Memento
       4:     Original original = new Original();
       5:     MementoData memento = original.CreateMemento();
       6:  
       7:     original.Offset(6);
       8:     original.Resume(memento);
       9:     #endregion
      10:  
      11:     Console.ReadLine();
      12: }

    Spy

  • 相关阅读:
    ubuntu文件夹建立软链接方法
    编译android内核和文件系统,已经安装jdk,提示build/core/config.mk:268: *** Error: could not find jdk tools.jar
    ubuntu12.04配置NFS服务详解
    解决ubuntu10.04不能上网
    GC
    IO
    HashMap
    JavaBean的介绍
    SSO二 之CAS
    SSO一 之介绍
  • 原文地址:https://www.cnblogs.com/Autumoon/p/1078571.html
Copyright © 2011-2022 走看看