201802262211->深入浅出设计模式:c#备忘录模式
2018-02-26 22:11
603 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _017备忘录模式
{
/// <summary>
/// 样本
/// </summary>
public class Originate
{
public string State;
/// <summary>
/// 备份
/// </summary>
/// <returns></returns>
public Memento CareTaker()
{
return new Memento(State);
}
}
/// <summary>
/// 备份处
/// </summary>
public class Memento
{
private string State;
public Memento(string state)
{
this.State = state;
}
public string getState()
{
return this.State;
}
}
internal class Program
{
private static void Main(string[] args)
{
Originate orginate = new Originate();
orginate.State = "on";
Console.WriteLine(orginate.State);
Memento memento = orginate.CareTaker();
orginate.State = "off";
Console.WriteLine(orginate.State);
orginate.State = memento.getState();
Console.WriteLine(orginate.State);
/*
* 备忘录模式
*
* 其实就是将一个对象再拷贝一次,以达到备份的目的
*
* 但是备份的对象可以拓展,这样能提高开发效率
*
* 第一次接触这模式,感觉有点直接和有点奇妙
*
*/
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _017备忘录模式
{
/// <summary>
/// 样本
/// </summary>
public class Originate
{
public string State;
/// <summary>
/// 备份
/// </summary>
/// <returns></returns>
public Memento CareTaker()
{
return new Memento(State);
}
}
/// <summary>
/// 备份处
/// </summary>
public class Memento
{
private string State;
public Memento(string state)
{
this.State = state;
}
public string getState()
{
return this.State;
}
}
internal class Program
{
private static void Main(string[] args)
{
Originate orginate = new Originate();
orginate.State = "on";
Console.WriteLine(orginate.State);
Memento memento = orginate.CareTaker();
orginate.State = "off";
Console.WriteLine(orginate.State);
orginate.State = memento.getState();
Console.WriteLine(orginate.State);
/*
* 备忘录模式
*
* 其实就是将一个对象再拷贝一次,以达到备份的目的
*
* 但是备份的对象可以拓展,这样能提高开发效率
*
* 第一次接触这模式,感觉有点直接和有点奇妙
*
*/
Console.ReadKey();
}
}
}
相关文章推荐
- 201802140000->深入浅出设计模式:c#高并发下单例模式
- 201802170025->深入浅出设计模式:c#组合模式
- 201802141508->深入浅出设计模式:c#创建者模式
- 201802191230->深入浅出设计模式:c#代理模式
- 201802282138->深入浅出设计模式:c#访问者模式
- 201802121524->深入浅出设计模式:c#简单工厂模式
- 201802181505->深入浅出设计模式:c#装饰者模式
- 201802181721->深入浅出设计模式:c#享元模式
- 201802272223->深入浅出设计模式:c#状态模式
- 201802272238->深入浅出设计模式:c#模板模式
- 201802132228->深入浅出设计模式:c#抽象工厂模式
- 201802151200->深入浅出设计模式:c#适配器模式
- 201802151313->深入浅出设计模式:c#桥模式
- 201802181537->深入浅出设计模式:c#外观模式
- 201802272130->深入浅出设计模式:c#观察者模式
- 201802141601->深入浅出设计模式:c#原型模式
- 201802191355->深入浅出设计模式:c#迭代器模式
- 201802191355->深入浅出设计模式:c#解析器模式
- 201802242302->深入浅出设计模式:c#调停者模式
- 《模式——工程化实现及扩展》(设计模式C# 版)《备忘录模式 Memento》——“自我检验" 参考答案