0

I need to write a Graph class which performs some sort of operations and gives possibility to save it using memento pattern. I am trying to implement that functionality, but I want to have IGraph interface which cannot return explicit type of ConcreteMemento so it returns Memento interface, but because of that I need to downcast it inside Graph class.

So my question is, is it a good practise or am I missing something in memento pattern? Also is it possible to implement ConcreteMemento in the way that only Graph can access some of its methods?

Grahp

class ConcreteMemento : Memento
{

}
internal class Graph : IGraph
{
    public Memento GetMemento()
    {
        return new ConcreteMemento();
    }

    public void SetMemento(Memento m)
    {
        var concreteMemento = m as ConcreteMemento;
    }
}

IGraph

public interface Memento
{

}
interface IGraph
{
    Memento GetMemento();
}
wohlstad
  • 12,661
  • 10
  • 26
  • 39
complikator
  • 235
  • 2
  • 8
  • [Refactoring Guru](https://refactoring.guru/design-patterns/memento) has an excellent article explaining this. Specifically see the section under _Structure_ where three alternative implementations are shown. – jaco0646 Apr 06 '22 at 00:16
  • Thanks, but it's actually not what I am asking about. He has two possible implementations of this pattern but first is the case above, second does not allow me to for example save state to file (because I assume that app can be restarted), but thank you for responding @jaco0646 – complikator Apr 07 '22 at 18:36
  • Why do you say, "two possible implementations" when the article clearly shows three? – jaco0646 Apr 07 '22 at 19:51

0 Answers0