title: What is the need for Memento Design Pattern in Calculator undo scenario
body: <p>I am reading about Memento design pattern. I came across an example which talks about Calculator undo functionality.</p>
  
  <p>I am able to achieve the Calculator Undo with the below code: </p>
  
  <p><code>Calculator</code></p>
  
  <pre><code>public class Calculator implements Cloneable {
  
  int num1;
  int num2;
  int result;
  
  Stack&lt;Calculator&gt; states = new Stack&lt;&gt;();
  
  public void setNum1(int num1) {
      this.num1 = num1;
  }
  
  public void setNum2(int num2) {
      this.num2 = num2;
  }
  
  public void setResult(int result) {
      this.result = result;
  }
  
  public int add() throws CloneNotSupportedException{
      result = num1 + num2;
      states.add((Calculator) this.clone());
      System.out.println("Caclulation done. ");
      return result;
  }
  
  public void undo(){
      states.pop();
      Calculator calc = states.peek();
      this.setNum1(calc.num1);
      this.setNum2(calc.num2);
      this.setResult(calc.result);
      System.out.println("Undo done. ");
  }
  
  public void displayState(){
      System.out.println("Current State: " + num1 + " + " + num2 + " = " + result);
  }
  }
  </code></pre>
  
  <p><code>CalculatorTest</code></p>
  
  <pre><code>    Calculator calc = new Calculator();
  
      calc.setNum1(10);
      calc.setNum2(11);
      calc.add();
      calc.displayState();
  
      calc.setNum1(12);
      calc.setNum2(13);
      calc.add();
      calc.displayState();
  
      calc.setNum1(16);
      calc.setNum2(17);
      calc.add();
      calc.displayState();
  
      calc.undo();
      calc.displayState();
  
      calc.undo();
      calc.displayState();
  </code></pre>
  
  <p><code>Output</code></p>
  
  <blockquote>
  <pre><code>Caclulation done. 
  Current State: 10 + 11 = 21
  Caclulation done. 
  Current State: 12 + 13 = 25
  Caclulation done. 
  Current State: 16 + 17 = 33
  Undo done. 
  Current State: 12 + 13 = 25
  Undo done. 
  Current State: 10 + 11 = 21
  </code></pre>
  </blockquote>
  
  <p>It works well for the undo. Why would I use Memento pattern for this scenario? </p>