手机
当前位置:查字典教程网 >编程开发 >C#教程 >c# 备忘录模式
c# 备忘录模式
摘要:结构图:Memento类:保存状态的容器复制代码代码如下:classMemento{publicstringState{get;set;}p...

结构图:

c# 备忘录模式1

Memento类:保存状态的容器

复制代码 代码如下:

class Memento

{

public string State { get; set; }

public Memento(string state)

{

this.State = state;

}

}

Caretaker是保存Memento类:

复制代码 代码如下:

class Caretaker

{

public Memento Memento { get; set; }

}

Originator类就是需要保存状态的类:

复制代码 代码如下:

class Originator

{

public string State { get; set; }

public Memento CreateMemento()

{

return (new Memento(State));

}

public void SetMemento(Memento memento)

{

State = memento.State;

}

public void Show()

{

Console.WriteLine("State:" + State);

}

}

主函数调用:

复制代码 代码如下:

class Program

{

static void Main(string[] args)

{

Originator o = new Originator();

o.State = "On";

o.Show();

Caretaker c = new Caretaker();

c.Memento = o.CreateMemento();

o.State = "off";

o.Show();

o.SetMemento(c.Memento);

o.Show();

Console.ReadKey();

}

}

【c# 备忘录模式】相关文章:

解析C#设计模式编程中备忘录模式的运用

c# 值类型实例构造器

c# 获得局域网主机列表实例

c# 可选参数、命名参数

C# Base64编码函数

c# 引用类型和值类型

c# JSON返回格式的WEB SERVICE

c#匹配整数和小数的正则表达式

C# 观察者模式实例介绍

设计模式中的备忘录模式解析及相关C++实例应用

上一篇: c# 组合模式
精品推荐
分类导航