. Java Design Patterns . Java Design Patterns Memento

Java Design Patterns Memento

Memento (aka Token) Overview

One object stores another objects state.

DvdDetails.java - the Originator
(the class to be backed up)
Contains the inner class DvdMemento - the Memento

import java.util.ArrayList;
import java.util.ListIterator;

//the originator
public class DvdDetails {  
   private String titleName;  
   private ArrayList stars;
   private char encodingRegion;
    
   public DvdDetails(String titleName, 
                     ArrayList stars, 
                     char encodingRegion) {
       this.setTitleName(titleName);
       this.setStars(stars);
       this.setEncodingRegion(encodingRegion);
   }    
  
   private void setTitleName(String titleNameIn) {
       this.titleName = titleNameIn;
   }
   private String getTitleName() {
       return this.titleName;
   }
   
   private void setStars(ArrayList starsIn) {
       this.stars = starsIn;
   }
   public void addStar(String starIn) {
       stars.add(starIn);
   }
   private ArrayList getStars() {
       return this.stars;
   }
   private static String getStarsString(ArrayList starsIn) {
       int count = 0;
       StringBuffer sb = new StringBuffer();
       ListIterator starsIterator = starsIn.listIterator();
       while (starsIterator.hasNext()) {
           if (count++ > 0) {sb.append(", ");} 
           sb.append((String) starsIterator.next());
       }
       return sb.toString();
   } 
   
   private void setEncodingRegion(char encodingRegionIn) {
      this.encodingRegion = encodingRegionIn;
   }
   private char getEncodingRegion() {
       return this.encodingRegion;
   }  
   
   public String formatDvdDetails() {
       return ("DVD: " + this.getTitleName() + 
               ", starring: " + getStarsString(getStars()) + 
               ", encoding region: " + this.getEncodingRegion()); 
   }   
   
   //sets current state to what DvdMemento has
   public void setDvdMemento(DvdMemento dvdMementoIn) {
       dvdMementoIn.getState();
   }
   //save current state of DvdDetails in a DvdMemento
   public DvdMemento createDvdMemento() {
       DvdMemento mementoToReturn = new DvdMemento();
       mementoToReturn.setState();
       return mementoToReturn;
   }
   
   //an inner class for the memento
   class DvdMemento {
       private String mementoTitleName;  
       private ArrayList mementoStars;
       private char mementoEncodingRegion;       
       
       //sets DvdMementoData to DvdDetails
       public void setState() {
            //Because String are immutable we can just set 
            //  the DvdMemento Strings to = the DvdDetail Strings.
            mementoTitleName = getTitleName();
            mementoEncodingRegion = getEncodingRegion();
            //However, ArrayLists are not immutable,
            //  so we need to instantiate a new ArrayList. 
            mementoStars = new ArrayList(getStars());
       } 
       //resets DvdDetails to DvdMementoData
       public void getState() {
            setTitleName(mementoTitleName);
            setStars(mementoStars);
            setEncodingRegion(mementoEncodingRegion);  
       }    
       
       //only useful for testing
       public String showMemento() {
            return ("DVD: " + mementoTitleName + 
                    ", starring: " + getStarsString(mementoStars) + 
                    ", encoding region: " + mementoEncodingRegion); 
       }      
   }
}
download source, use right-click and "Save Target As..." to save with a .java extension.

TestDvdMemento.java - testing the Memento - contains a Caretaker object

import java.util.ArrayList;

public class TestDvdMemento {
   public static void main(String[] args) {
       DvdDetails.DvdMemento dvdMementoCaretaker;  
       //the Caretaker
       
       ArrayList stars = new ArrayList();
       stars.add(new String("Guy Pearce"));
       DvdDetails dvdDetails = 
         new DvdDetails("Memento", stars, '1'); 
       dvdMementoCaretaker = dvdDetails.createDvdMemento();
       System.out.println("as first instantiated");
       System.out.println(dvdDetails.formatDvdDetails());
       
       System.out.println("");       
       dvdDetails.addStar("edskdzkvdfb");  
       //oops - Cappuccino on the keyboard!
       System.out.println("after star added incorrectly");
       System.out.println(dvdDetails.formatDvdDetails());
       
       System.out.println("");
       System.out.println("the memento");
       System.out.println(dvdMementoCaretaker.showMemento());
       //show the memento
       
       System.out.println("");
       dvdDetails.setDvdMemento(dvdMementoCaretaker);
       //back off changes
       System.out.println(
         "after DvdMemento state is restored to DvdDetails");
       System.out.println(dvdDetails.formatDvdDetails());
   }
}      
download source, use right-click and "Save Target As..." to save with a .java extension.

Test Results

as first instantiated
DVD: Memento, starring: Guy Pearce, encoding region: 1

after title set incorrectly DVD: Memento, starring: Guy Pearce, edskdzkvdfb, encoding region: 1

the memento DVD: Memento, starring: Guy Pearce, encoding region: 1

after DvdMemento state is restored to DvdDetails DVD: Memento, starring: Guy Pearce, encoding region: 1

UML

UML for Memento

References

online

Portland Pattern Repository

Books

Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
Java Design Patterns - A Tutorial by James W. Cooper
Comments Comments are left by visitors to FluffyCat.com, are not endorsed by FluffyCat.com, and may or may not be accurate.
Comment by archive Rate this Comment

I realy appreciate the Java Patterns on your site. Since I like to play with the examples I simply copied it into text fils and compiled them. I found a few minor problems you may want to know about:

- The test Results are different than shown:

DVD: Memento, starring: Guy Pearce, encoding
region: 1

>>> after star added incorrectly
>>> DVD: Memento, starring: Guy Pearce,
edskdzkvdfb, encoding region: 1

the memento
DVD: Memento, starring: Guy Pearce, encoding
region: 1

after DvdMemento state is restored to DvdDetails
DVD: Memento, starring: Guy Pearce, encoding
region: 1

Sign In
to add your own comment