c# 备忘录模式
程序员文章站
2023-12-04 22:24:58
结构图: memento类:保存状态的容器复制代码 代码如下: class memento {&nb...
结构图:
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();
}
}
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();
}
}
上一篇: python实现基本进制转换的方法
下一篇: vue多层嵌套路由实例分析