Memo mode of C# design mode

memo mode


This blog will introduce the memo mode. The memo mode is the "moonlight box" in the software system. It provides an object state revocation implementation mechanism. When an object in the system needs to restore a certain historical state, you can use the memo mode for design.

Pattern Classification

Behavioral Design Patterns.

The reason for the pattern

In the process of using the software, you will inevitably have some misoperations, such as accidentally deleting some text or pictures. In order to make the software more user-friendly, it is necessary to provide a mechanism similar to regret medicine for these misoperations, so that the software system It can return to the state before the misoperation, so it is necessary to save the state of the system during each operation of the user. Once a misoperation occurs, it only needs to take out the stored historical state to return to the previous state. The memento pattern was born for such problems.

Pattern Class Diagram

insert image description here

As can be seen from the figure above, the memo pattern consists of the following three objects:

Originator:

The originator is a common class. Generally, we let the class that needs to be backed up exist as the originator. The originator can save the current system state by creating a memo, or use the memo to restore its internal state.

Memento:

The memo is used to store the internal state of the originator, and the design of the memo usually refers to the design of the originator. It should be noted that except for the Caretaker class and the Originator class, other classes in the system cannot access the content of the memo.

Caretaker (in charge of humans):

A memorandum of responsibility for the human responsible for managing the system.

Code

We simulate the undo and redo functions of word through the memo mode.

NotePad originator:

using System;

namespace Memento.Memento.Question5
{
    
    
    public class NotePad
    {
    
    
        private string _content;

        public void ChangeContent(string content)
        {
    
    
            _content = content;
            SaveContent(content);
            Show();
        }

        public void UndoContent()
        {
    
    
            _content = Caretaker.GetInstance.UndoPop().GetContent();
            Show();
        }

        public void RedoContent()
        {
    
    
            _content = Caretaker.GetInstance.RedoPop().GetContent();
            Show();
        }

        public void Show()
        {
    
    
            Console.WriteLine(_content);
        }

        private void SaveContent(string content)
        {
    
    
            Caretaker.GetInstance.UndoPush(new Memento(content));
        }
    }
}

Memento memo:

namespace Memento.Memento.Question5
{
    
    
    public class Memento
    {
    
    
        private string _content;

        public Memento(string content)
        {
    
    
            _content = content;
        }

        public void SetContent(string content)
        {
    
    
            _content = content;
        }

        public string GetContent()
        {
    
    
            return _content;
        }
    }
}

Caretaker is responsible for humans:

using System;
using System.Collections.Generic;

namespace Memento.Memento.Question5
{
    
    
    public class Caretaker
    {
    
    
        private static Caretaker _instance;

        public static Caretaker GetInstance
        {
    
    
            get
            {
    
    
                if (_instance == null)
                {
    
    
                    _instance = new Caretaker();
                }

                return _instance;
            }
        }
        
        private  Stack<Memento> _undoStack = new Stack<Memento>();
        private  Stack<Memento> _redoStack = new Stack<Memento>();

        public void UndoPush(Memento memento)
        {
    
    
            _undoStack.Push(memento);
        }

        public Memento UndoPop()
        {
    
    
            if (_undoStack.Count == 0)
            {
    
    
                Console.WriteLine($"无法继续撤销");
            }
            Memento memento = _undoStack.Pop();
            RedoPush(memento);
            return memento;
        }

        public void RedoPush(Memento memento)
        {
    
    
            _redoStack.Push(memento);
        }

        public Memento RedoPop()
        {
    
    
            if (_redoStack.Count == 0)
            {
    
    
                Console.WriteLine($"无法继续重做");
            }
            Memento memento = _redoStack.Pop();
            UndoPush(memento);
            return memento;
        }
    }
}

Program class:

using System;
using Memento.Memento.Question5;

namespace Memento
{
    
    
    internal class Program
    {
    
    
        public static void Main(string[] args)
        {
    
    
            NotePad notePad = new NotePad();
            notePad.ChangeContent("123");
            notePad.ChangeContent("1234");
            notePad.UndoContent();
            notePad.UndoContent();
            notePad.RedoContent();
            notePad.RedoContent();
            notePad.ChangeContent("12345");
            notePad.ChangeContent("123456");
            notePad.UndoContent();
            notePad.UndoContent();
            notePad.ChangeContent("1234567");
            notePad.ChangeContent("12345678");
            notePad.UndoContent();
            notePad.UndoContent();
            notePad.UndoContent();
            notePad.UndoContent();
        }
    }
}

The principle is such a principle, but the above implementation has a small problem. Every time it is redone or undone, the top layer stores the current state.

Memo Pattern Summary

Advantages of the memo pattern:

  1. The Memento mode provides a mechanism for implementing state recovery, allowing users to return to a specific historical state conveniently.
  2. The memo mode realizes the encapsulation of information. A memo object is a state representation of an originator object and will not be changed by other codes. Generally, in order to achieve this in C#, we usually distinguish it through assemblies.

Disadvantages of memento pattern:

  1. The classic memo mode consumes a lot of computer resources.

Guess you like

Origin blog.csdn.net/BraveRunTo/article/details/119139440