Newsgroup: it.comp.java
Titolo: JAVA MEMENTO/SNAPSHOT PATTERN

Hi,
Someone got more information about The snapshot/memento pattern... The
exact thing that I want is a good example code... and also more texts
are always welcome
you can always contact me at 
je...@beer.com
or just leave a message at the newsgroup!
thx

are always welcome
I guess u know the basic idea: capturing an opaque vision of the state of an 
object to be able to revert to such state instantly at some later point with 
any sort of control - doing so without exposing the details of the state 
outside the Snapshot object itself.
Some skeleton but working code follows. Note that no code external 
to the snapshot or the Provider object has access to the object state,
and this holds for  the Controller object(s) too. 
This version has a strong coupling between the Snapshot and the 
Provider object, which translates directly into the use of inner classes.
// --- General (framework?) entities
public interface Snapshot {
  public void set(SnapshotProvider sp);
};
public interface SnapshotProvider {
 public Snapshot getSnapshot();
 public void reset(Snapshot ss);
};
// This controller keeps trace of just one object-Snapshot relationship
// It may be made a singleton and augmented to keep trace of n such
// relationships.
class SnapshotController {
 // For simplicity, we don't propagate exceptions
 class InvalidSnapshotException extends RuntimeException {
  public InvalidSnapshotException() {
   super("The snapshot has not been originated by this object"); 
  }
 }
 private SnapshotProvider originator;
 public SnapshotController(SnapshotProvider originator) {
   this.originator=originator;
 }
 public void check(SnapshotProvider sp) throws InvalidSnapshotException {
   // You can choose the constraint you want here  for reapplying a snapshot.
   // Here a snaspshot can be reapplied only to exactly the same object.
   if (sp != originator)     	    	    	    	    
     throw new InvalidSnapshotException();
 }
}
// A default base implementation
public abstract class BaseSnapshot implements Snapshot { 
  private SnapshotController controller;
  public BaseSnapshot(SnapshotProvider sp) {
    	controller=new SnapshotController(sp);
  }
 
  public void set(SnapshotProvider sp) {
  	controller.check(sp);
  	reset(sp);
  }
 
  public abstract void reset(SnapshotProvider sp);
}
// ---- end of General entities
// A concrete object which provides and accepts snapshots
class Obj implements SnapshotProvider { 
 // A class to hold the state, avoid re-declaration in the snapshot object
 // State types must be Cloneable. 
 // For shortness' sake, it's built like a struct
 private class ObjState implements Cloneable {
   public int state1;
   public String state2;
   public ObjState createCopy() {
     try {
      return (ObjState)clone();
     } catch (CloneNotSupportedException e) {
     	e.printStackTrace();
     	throw new RuntimeException("Cannot create state clone");
     }
   }
 }
 // The state of the class
 private ObjState os;
 // The snapshot for this particular class
 private class ObjSnapshot extends BaseSnapshot {
     ObjState state;
    	public ObjSnapshot() {
    	    	super(Obj.this);
    	    	state=os.createCopy(); // Saves state
    	}
	// Restores state
    	public void reset(SnapshotProvider sp) { 
    	  os.state1=state.state1; 
    	  os.state2=state.state2;
    	}
 }
 
 // Implements SnapshotProvider
 public Snapshot getSnapshot() { return new ObjSnapshot();  }
 public void reset(Snapshot ss) { ss.set(this); }
 
 // Obj's constructor with test initializers
 public Obj(int initialState1, String initialState2) {
 	os=new ObjState();
	os.state1=initialState1;
	os.state2=initialState2;
 }
 
 public void view() {
  System.out.println("Obj state: State 1 = "+
                      os.state1+
                     ", State 2 = \""+
                      os.state2+"\"");
 }
 
 public void evolve() {
 	os.state1+=10;
 	os.state2+=" World";
 }
 
}
// A test class
class SnapshotTest {
 public static void main(String args[]) {
 
   System.out.println("Creating an Obj..");
   Obj obj= new Obj(5, "Hello");
   obj.view();
 
   System.out.println("Getting Snapshot");
   Snapshot ss = obj.getSnapshot();
 
   System.out.println("Obj evolves its state..");
   obj.evolve();
   obj.view();
 
   System.out.println("Using snapshot to reset state..");
   obj.reset(ss);
   obj.view();
 }
}
-- 
God is Real, unless declared Integer.
ObjectZone - 
http://space.tin.it/computer/csadun/