📚
Lerndokumentationen
.NET
.NET
  • Willkommen
  • C#
    • Variablen
      • Strings
      • Integers
      • Floats und Doubles
      • Booleans
      • Casting und Parsing
    • Kontrollstrukturen
      • if-Statements
      • Ternary-Operator
      • Switch-Statements und -Expressions
    • Collections
      • Arrays
      • Lists
      • Dictionaries
    • Loops
      • while-Loops
      • for-Loops
      • foreach-Loops
      • continue und break
    • Methoden
      • Callbacks und Delegates
      • Extension Methods
      • Lazy
    • Objektorientierte Programmierung
      • Fields und Properties
      • Methoden
      • Statisch vs. Instanzen
      • Konstruktoren
      • Vererbung
      • Komposition
      • Generics
      • Tupel
    • Referenz- und Wertetypen
      • Enums
      • Structs
      • Records
    • Binary und Strings
      • Kodierung von Strings und Bytes
      • Streams
      • XML und JSON
  • Design Patterns
    • Creational Patterns
      • Singleton
    • Structural Patterns
      • Decorator Pattern
    • Behavioral Patterns
      • Memento
      • Strategy
Bereitgestellt von GitBook
Auf dieser Seite
  • Klassendiagramm
  • Implementierung
  • Beispiel
  • Klassendiagramm
  • Implementierung
  1. Design Patterns
  2. Behavioral Patterns

Memento

VorherigeBehavioral PatternsNächsteStrategy

Zuletzt aktualisiert vor 1 Monat

Mit dem Memento-Pattern kann man ohne die Kapselung zu verletzen, den internen Zustand eines Objekts erfassen und externalisieren, so dass das Objekt später wieder in diesen Zustand versetzt werden kann.

Klassendiagramm

Implementierung

Originator.cs
public class Originator
{
    private string _state = "";

    public void SetState(string state)
    {
        _state = state;
    }

    public string GetState()
    {
        return _state;
    }

    public Memento CreateMemento()
    {
        return new(_state);
    }

    public void Restore(Memento memento)
    {
        _state = memento.GetState();
    }
}
Memento.cs
public class Memento
{
    private readonly string _state;

    public Memento(string state)
    {
        _state = state;
    }

    public string GetState()
    {
        return _state;
    }
}
Caretaker.cs
public class Caretaker
{
    private readonly List<Memento> _mementos = [];

    public void AddMemento(Memento memento)
    {
        _mementos.Add(memento);
    }

    public Memento GetMemento(int index)
    {
        return _mementos[index];
    }
}

Beispiel

Klassendiagramm

Implementierung

TextBox.cs
public class TextBox
{
    private string _text = "";

    public void SetText(string text)
    {
        _text = text;
    }

    public string GetText()
    {
        return _text;
    }

    public TextState Save()
    {
        return new(_text);
    }

    public void Restore(TextState textState)
    {
        _text = textState.GetText();
    }
}
TextState.cs
public class TextState
{
    private readonly string _text;

    internal TextState(string text)
    {
        _text = text;
    }

    internal string GetText()
    {
        return _text;
    }
}
TextHistory.cs
public class TextHistory
{
    private readonly Stack<TextState> _undoStack = [];
    private readonly Stack<TextState> _redoStack = [];

    public void Backup(TextState textState)
    {
        _undoStack.Push(textState);
        _redoStack.Clear();
    }

    public void Undo(TextBox textBox)
    {
        if (_undoStack.Count < 1)
        {
            return;
        }

        _redoStack.Push(_undoStack.Pop());
        textBox.Restore(_undoStack.Peek());
    }

    public void Redo(TextBox textBox)
    {
        if (_redoStack.Count < 1)
        {
            return;
        }

        var redoState = _redoStack.Pop();

        _undoStack.Push(redoState);
        textBox.Restore(redoState);
    }
}