The Memento design pattern without violating encapsulation, captures and externalizes an object‘s internal state so that the object can be restored to this state later.
备忘录模式让程序能捕获外部的变化并改变内部的状态,有点像是游戏读取存档的功能~
学习目标: 备忘录模式的概念及实务
学习难度: ☆☆☆
using System;
namespace ConsoleApp1
{
public class Player
{
int money;
public int Money
{
get { return money; }
set
{
money = value;
Console.WriteLine("Player's Money = " + money);
}
}
public Memento CreateMemento()
{
return (new Memento(money));
}
public void SetMemento(Memento memento)
{
Console.WriteLine("Return to last data...");
Money = memento.Money;
}
}
public class Memento
{
int money;
public Memento(int money)
{
this.money = money;
}
public int Money
{
get { return money; }
}
}
public class Storage
{
Memento memento;
public Memento Memento
{
set { memento = value; }
get { return memento; }
}
}
public class MainProgram
{
public static void Main(string[] args)
{
Player player = new Player();
player.Money = 100;
Storage storage = new Storage();
storage.Memento = player.CreateMemento();
player.Money = 50;
player.SetMemento(storage.Memento);
}
}
}
参考资料:
https://www.dofactory.com/net/memento-design-pattern
<<: 【C#】Behavioral Patterns Chain of Responsibility Mode
>>: 【报错解法】load_dataset 没读到资料 - FileNotFoundError: Unable to resolve any data file that matches
文章参考自 https://juejin.im/post/6844903744518389768 h...
filter filter() 会建立一个新的阵列,其内容为原阵列的每一个元素经由回呼函式判断後所回...
资料库是开发应用程序非常重要的储存工具,可以储存各种资讯,还可以快速的查询出想要的资讯。 微软在 2...
引注资料 https://blog.csdn.net/weixin_43184622/article...
题目来源:邦友问答,因觉得有趣就尝试推论看看 python 多赋值是如何运作的 以下是我推论出来的,...