October 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working With Design Patterns: Memento

  • January 10, 2008
  • By Jeff Langr
  • Send Email »
  • More Articles »

The only real unique part of each command is its ability to transform the canvas by drawing a polygon (perhaps "transform" was not the best method name I could have chosen). The SquareCommand code in Listing 7 shows what one command looks like. TriangleCommand isn't much different.

Listing 7: SquareCommand.

import java.awt.*;
import java.awt.geom.*;
import model.*;

public class SquareCommand extends AbstractCommand {
   private static final int SIZE = 12;

   public SquareCommand(PathCanvas canvas, Dimension size,
      Color color) {
      super(canvas, size.getWidth(), size.getHeight(), color);
   }

   @Override
   public void transform() {
      canvas.add(new ColoredPath(createSquare(), color));
   }

   private GeneralPath createSquare() {
      GeneralPath path = new GeneralPath();
      double x = random(width);
      double y = random(height);
      path.moveTo(x, y);
      path.lineTo(x + SIZE, y);
      path.lineTo(x + SIZE, y + SIZE);
      path.lineTo(x, y + SIZE);
      path.lineTo(x, y);
      path.closePath();
      return path;
   }
}

The real "meat" of the memento pattern is in the three Command implementation methods:

public void execute() {
   this.memento = canvas.getMemento();
   transform();
}

public void undo() {
   Memento redoMemento = canvas.getMemento();
   canvas.setMemento(memento);
   memento = redoMemento;
}

public void redo() {
   Memento undoMemento = canvas.getMemento();
   canvas.setMemento(memento);
   memento = undoMemento;
}

Prior to execution, the command object stores a memento. For undo, the command object obtains the current canvas state (into redoMemento), changes the state to the previously stored memento, and then stores the redoMemento object so that a subsequent redo operation will work. The redo method works similarly.

The memento pattern (see the UML diagram in Figure 1) is really straightforward. Design patterns don't always need to be complex. Sometimes, the most important thing that comes out of design patterns is that they simplify things by providing a consistent solution with recognizable class names.

I've attached the code for your perusal. The lessons always seem to sink in a little better when you can see the code actually working. The attached zip file contains complete source for a simple user interface that demonstrates drawing polygons, as well as undoing and redoing along the way!



Click here for a larger image.

Figure 1: Memento

Download the Code

You can download the code that accompanies this article here.

About the Author

Jeff Langr is a veteran software developer with over a quarter century of professional software development experience. He's written two books, including Agile Java: Crafting Code With Test-Driven Development (Prentice Hall) in 2005. Jeff is contributing a chapter to Uncle Bob Martin's upcoming book, Clean Code. Jeff has written over 50 published articles on software development, with more than a couple dozen appearing at Developer.com. You can find out more about Jeff at his site, http://langrsoft.com, or you can contact him via email at jeff at langrsoft dot com.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel