手机
当前位置:查字典教程网 >编程开发 >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# 备忘录模式】相关文章:

详解Objective-C设计模式编程中对备忘录模式的运用

深入c#工厂模式的详解

c# 组合模式

c# 类型转换

C++设计模式之备忘录模式

c# 适配器模式

C# Base64编码函数

c# 类型构造器

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

c# 删除所有的空文件夹的小例子

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