zoukankan      html  css  js  c++  java
  • 设计模式学习笔记--备忘录模式

     1 using System;
     2 
     3 namespace Memento
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/29 6:56:24 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Originator说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class Originator
    12     {
    13         private string state;
    14 
    15         public string State
    16         {
    17             get { return state; }
    18             set { state = value; }
    19         }
    20 
    21         public Memento CreateMemento()
    22         {
    23             return new Memento(state);
    24         }
    25 
    26         public void SetMemento(Memento memento)
    27         {
    28             this.state = memento.State;
    29         }
    30 
    31         public void Show()
    32         {
    33             Console.WriteLine("state=" + state);
    34         }
    35     }
    36 }
    View Code
     1 using System;
     2 
     3 namespace Memento
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/29 6:54:54 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Memento说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class Memento
    12     {
    13         private string state;
    14 
    15         public string State
    16         {
    17             get { return state; }
    18         }
    19 
    20         public Memento(string state)
    21         {
    22             this.state = state;
    23         }
    24     }
    25 }
    View Code
     1 using System;
     2 
     3 namespace Memento
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/29 6:59:40 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Caretaker说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class Caretaker
    12     {
    13         private Memento memento;
    14 
    15         public Memento Memento
    16         {
    17             get { return memento; }
    18             set { memento = value; }
    19         }
    20     }
    21 }
    View Code
     1 namespace Memento
     2 {
     3     class Program
     4     {
     5         static void Main(string[] args)
     6         {
     7             Originator originator = new Originator();
     8             originator.State = "On";
     9             originator.Show();
    10 
    11             Caretaker caretaker = new Caretaker();
    12             caretaker.Memento = originator.CreateMemento();
    13 
    14             originator.State = "Off";
    15             originator.Show();
    16 
    17             originator.SetMemento(caretaker.Memento);
    18             originator.Show();
    19         }
    20     }
    21 }
    View Code
  • 相关阅读:
    Linux Sever简单笔记(第十二堂课)之linux下的系统故障分析和排查
    Linux Sever简单笔记(第十一堂课)之linux下的备份和恢复及rsync还有inotify和dump以及restore
    Linux Sever简单笔记(第十堂课)之linux下的任务计划及相关的命令
    ubuntu18.04设置apt源(国内)
    shell简单常用脚本实例
    装完ubuntu系统之后,不能ssh正常连接
    mysql主从复制以及读写分离
    复习计划
    linux下dhcp的安装及配置
    日常问题
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5538838.html
Copyright © 2011-2022 走看看