Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In my humble opinion, you're thinking it in a more complicated way than it really is. In order to revert to previous state, clone of whole node is not required at all. Rather each <em>*</em>*Command class will have -</p> <ol> <li>reference to the node it is acting upon, </li> <li>memento object (having state variables just enough for the node to revert to)</li> <li>execute() method </li> <li>undo() method.</li> </ol> <p>Since command classes have reference to the node, we do not need ID mechanism to refer to objects in the diagram.</p> <p>In the example from your question, we want to move a node to a new position. For that, we have a NodePositionChangeCommand class.</p> <pre><code>public class NodePositionChangeCommand { // This command will act upon this node private Node node; // Old state is stored here private NodePositionMemento previousNodePosition; NodePositionChangeCommand(Node node) { this.node = node; } public void execute(NodePositionMemento newPosition) { // Save current state in memento object previousNodePosition // Act upon this.node } public void undo() { // Update this.node object with values from this.previousNodePosition } } </code></pre> <blockquote> <blockquote> <p>What about links? They should be movable too but I don't want to create a command just for links (and one just for nodes).</p> </blockquote> </blockquote> <p>I read in GoF book (in memento pattern discussion) that move of link with change in position of nodes are handled by some kind of constraint solver.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload