CSharp · Console
using System;
using System.Collections.Generic;
public class EditorState
{
private string content;
public EditorState(string content) {
this.content = content;
}
public string Content {
get
{
return content;
}
}
}
public class Editor
{
public string Content {get; set; }
public EditorState createState() {
return new EditorState(Content);
}
public void restore(EditorState state) {
Content = state.Content;
}
}
public class History
{
private List<EditorState> states = new List<EditorState>();
public void push(EditorState state) {
states.Add(state);
}
public EditorState pop() {
var lastIndex = states.Count - 1;
var lastState = states[lastIndex];
states.Remove(lastState);
return lastState;
}
}
public class Program
{
public static void Main()
{
var editor = new Editor();
var history = new History();
editor.Content = "hello";
history.push(editor.createState());
editor.Content = "hello world";
history.push(editor.createState());
editor.Content = "hello world design patterns";
editor.restore(history.pop());
Console.WriteLine(editor.Content);
}
}