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

     1 /*
     2  * Window系统可能会异常终止,设计一个系统备份程序。类WindowsSystem是
     3  * 发起人角色(Orignation),类Memento是备忘录角色(Memento),类User是
     4  * 备忘录管理角色(Caretaker)。应用备忘录模式,用C#控制台应用程序实现
     5  * 该设计。
     6  */
     7 using System;
     8 using System.Collections.Generic;
     9 using System.Linq;
    10 using System.Text;
    11 
    12 namespace Memento
    13 {
    14     //Orignation
    15     class WindowsSystem
    16     {
    17         private string systemstate;
    18         public string SystemState
    19         {
    20             get { return systemstate; }
    21             set { systemstate = value; }
    22         }
    23         public Memento SaveState()
    24         {
    25             return (new Memento(systemstate));
    26         }
    27         public void RecoveryState(Memento memeto)
    28         {
    29             this.systemstate = memeto.SystemState;
    30         }
    31         public void Show()
    32         {
    33             Console.WriteLine("当前系统状态:"+systemstate);
    34         }
    35     }
    36     //Memento
    37     class Memento
    38     { 
    39         private string systemstate;
    40         public Memento(string systemstate)
    41         {
    42             this.systemstate = systemstate;
    43         }
    44         public string SystemState
    45         {
    46             get { return systemstate; }
    47         }
    48     }
    49     //Caretaker
    50     class User
    51     {
    52         private Memento memento;
    53         public Memento MementoFunc
    54         {
    55             get { return memento; }
    56             set { memento = value; }
    57         }
    58     }
    59     class Program
    60     {
    61         static void Main(string[] args)
    62         {
    63             //系统正常
    64             Console.WriteLine("系统启动");
    65             WindowsSystem ws = new WindowsSystem();
    66             ws.SystemState = "正常";
    67             ws.Show();
    68             //备份
    69             Console.WriteLine("系统备份");
    70             User user = new User();
    71             user.MementoFunc = ws.SaveState();
    72             //系统崩溃
    73             Console.WriteLine("系统崩溃");
    74             ws.SystemState = "崩溃";
    75             ws.Show();
    76             //系统恢复
    77             Console.WriteLine("系统恢复");
    78             ws.RecoveryState(user.MementoFunc);
    79             ws.Show();
    80         }
    81     }
    82 }
    字节跳动内推

    找我内推: 字节跳动各种岗位
    作者: ZH奶酪(张贺)
    邮箱: cheesezh@qq.com
    出处: http://www.cnblogs.com/CheeseZH/
    * 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    安装chrome driver(14)
    爬虫-selenium实现验证码自动登录(14)
    爬虫-反爬与反反爬(12)
    爬虫-模拟登录(13)
    爬虫-GIL与线程同步问题(11)
    爬虫-多进程(10)
    爬取csdn的数据与解析存储(9)
    Exchange Server 2016邮件系统建设方案
    Exchange 2016高可用及容灾架构选型参考
    Installing Exchange 2016 on Windows Server 2016 Step by Step
  • 原文地址:https://www.cnblogs.com/CheeseZH/p/2508490.html
Copyright © 2011-2022 走看看