The Memento Pattern; Java Serialization - Lecture Slides | CMPSCI 220, Study notes of Computer Science

Material Type: Notes; Class: Programming Methodology; Subject: Computer Science; University: University of Massachusetts - Amherst; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-8e4
koofers-user-8e4 🇺🇸

5

(1)

9 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST
Department of Computer Science
Department of Computer Science
14 – The Memento Pattern; Java Serialization
CMPSCI 220 (291A)
Programming Methodology
Spring 2009
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST
Department of Computer Science
Department of Computer Science 2
The Memento Pattern
Goals:
Save important state of a system’s key object …
While maintaining the key object’s encapsulation
Benefits:
Separating saved state from the key object maintains
cohesion and the key object’s encapsulation
Provides easy-to-implement recovery capability
Considerations:
Saving and restoring state can be time consuming
Java Serialization is a good implementation approach
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST
Department of Computer Science
Department of Computer Science 3
Memento Pattern
Client
//when important state is reached
Object saved =
(Object)ko.getCurrentState();
//when a restore is required
ko.restoreState(saved);
KeyObject ____
objectState _____
Object getCurrentState() {
//gather state
return(objectState);
}
restoreState(Object savedState); {
//restore state
}
// do other Key Object stuff
KeyObjectMemento
savedObjectState
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST
Department of Computer Science
Department of Computer Science 4
Memento Pattern in the Project
BreakoutGame/BOEngine
//when save is requested
GameSnapshot snapshot =
GameSnapshot.makeSnapshot(
(game, gameState); // **
//when restoring (a new game)
= new BreakoutGame(
gameEngine, snapshot); // ***
BreakoutGame/GameState
GameState snapshot
void saveGame() {
//gather state: code at **
}
BreakoutGame(Game Engine e,
GameSnapshot s); {
//restore state: code at ***
}
// do other Key Object stuff
GameSnapshot
savedBall
savedBlocks
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST
Department of Computer Science
Department of Computer Science 5
Making a Memento in Java
Often want to make a copy of an object
In Java, this is called cloning
Class Object has a default clone method
Signature: protected Object clone()
Creates a duplicate whose fields have the same values as
the original
This is a shallow copy (more in a moment)
Must implement the Cloneable marker interface in
order to clone an object
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST
Department of Computer Science
Department of Computer Science 6
Shallow versus Deep Copies
pf3
pf4
pf5
pf8

Partial preview of the text

Download The Memento Pattern; Java Serialization - Lecture Slides | CMPSCI 220 and more Study notes Computer Science in PDF only on Docsity!

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science

14 – The Memento Pattern; Java Serialization

CMPSCI 220 (291A) Programming Methodology Spring 2009 U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 2 The Memento Pattern „ Goals:

„ Save important state of a system’s key object …

„ While maintaining the key object’s encapsulation

„ Benefits:

„ Separating saved state from the key object maintains

cohesion and the key object’s encapsulation

„ Provides easy-to-implement recovery capability

„ Considerations:

„ Saving and restoring state can be time consuming

„ Java Serialization is a good implementation approach

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 3 Memento Pattern

Client

//when important state is reached

Object saved =

(Object)ko.getCurrentState();

//when a restore is required

ko.restoreState(saved);

KeyObject ____

objectState _____

Object getCurrentState() {

//gather state

return(objectState);

restoreState(Object savedState); {

//restore state

// do other Key Object stuff

KeyObjectMemento

savedObjectState

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 4 Memento Pattern in the Project

BreakoutGame/BOEngine

//when save is requested

GameSnapshot snapshot =

GameSnapshot.makeSnapshot(

(game, gameState); // **

//when restoring (a new game)

… = new BreakoutGame(

gameEngine, snapshot); // ***

BreakoutGame/GameState

GameState snapshot

void saveGame() {

//gather state: code at **

BreakoutGame(Game Engine e,

GameSnapshot s); {

//restore state: code at ***

// do other Key Object stuff

GameSnapshot

savedBall

savedBlocks

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 5 Making a Memento in Java „ Often want to make a copy of an object „ In Java, this is called cloning „ Class Object has a default clone method „ Signature: protected Object clone() „ Creates a duplicate whose fields have the same values as the original „ This is a shallow copy (more in a moment) „ Must implement the Cloneable marker interface in order to clone an object U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 6 Shallow versus Deep Copies

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 7 Shallow versus Deep Copies U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 8 Shallow versus Deep Copies UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 9 What if you need deep copying? „ Some classes need deep copying for proper semantics „ To get it, you must write your own clone() method, which can call clone() on the child objects

„ I.e., you override the default clone() that comes

from class Object

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 10 Game snapshot Mementos „ You need to clone the Sprites of the game

„ There is only one Ball and one Paddle (easy)

„ There can be many Blocks and PowerUps

„ Use SpriteGroup.getSprites() to obtain them „ Add non-null active Sprites to a List that is part of the Memento object’s state „ You will also need to clone the GameState „ You need to mark these Cloneable UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 11 Another detail „ In Object, clone() is marked protected

„ You may need to declare a public method:

public Object clone () throws CloneNotSupportedException { return super.clone(); } U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 12 Typical use Ball clonedBall = (Ball)ball.clone(); // must be in a method marked // “throws CloneNotSupportedException” // or in a try block: try { clonedBall = (Ball)ball.clone(); … } catch (CloneNotSupportedException exc) { // handle the exception (e.g., print it) }

A small example

5. Write: object

5. Write: object

5. Write: object

5. Write: object

5. Write: object

5. Write: object

6. Write: class

6. Write: class

6. Write: class

6. Write: class

6. Write: class
7. Write value of field y: 2.

7. Write value of field y: 2.

7. Write value of field y: 2.

7. Write value of field y: 2.

  1. Write: null
    1. Write: null
6. Read: class

6. Read: class

7. Read value of field y: 2.

7. Read value of field y: 2.

  1. Read: null
    1. Read: null
  • UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science
      1. Write: com.xyz.product.package.A, version
      • U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science
          1. Write: com.xyz.product.package.A, version A small example
          1. Write value of field x:
  • UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science
      1. Write: com.xyz.product.package.A, version A small example
      1. Write value of field x:
      1. Write: com.xyz.product.package.B, version
      • U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science
          1. Write: com.xyz.product.package.A, version A small example
          1. Write value of field x:
          1. Write: com.xyz.product.package.B, version
          1. Write value of field y: 1.
  • UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science
      1. Write: com.xyz.product.package.A, version A small example
      1. Write value of field x:
      1. Write: com.xyz.product.package.B, version
      1. Write value of field y: 1.
      1. Write: object #
      • U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science
          1. Write: com.xyz.product.package.A, version A small example
          1. Write value of field x:
          1. Write: com.xyz.product.package.B, version
          1. Write value of field y: 1.
          1. Write: object #
          1. Write: class #
  • UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science
      1. Write: com.xyz.product.package.A, version A small example
      1. Write value of field x:
      1. Write: com.xyz.product.package.B, version
      1. Write value of field y: 1.
      1. Write: object #
      1. Write: class #
      1. Write value of field y: 2.
      • U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science
          1. Write: com.xyz.product.package.A, version A small example
          1. Write value of field x:
          1. Write: com.xyz.product.package.B, version
          1. Write value of field y: 1.
          1. Write: object #
          1. Write: class #
          1. Write value of field y: 2.
  • UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 8. Write: null
      1. Write: com.xyz.product.package.A, version A small example
      1. Write value of field x:
      1. Write: com.xyz.product.package.B, version
      1. Write value of field y: 1.
      1. Write: object #
      1. Write: class #
      1. Write value of field y: 2.
      • U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 9. Write: null
          1. Write: com.xyz.product.package.A, version A small example
          1. Write value of field x:
          1. Write: com.xyz.product.package.B, version
          1. Write value of field y: 1.
          1. Write: object #
          1. Write: class #
          1. Write value of field y: 2.
          1. Write: object # 9. Write: null
  • UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science
      1. Read: com.xyz.product.package.A, version Reading it back
      1. Read value of field x:
      1. Read: com.xyz.product.package.B, version
      1. Read value of field y: 1.
      1. Read: object #
      1. Read: class #
      1. Read value of field y: 2.
      1. Read: object # 9. Read: null
      • U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science
          1. Read: com.xyz.product.package.A, version Reading it back
          1. Read value of field x:
          1. Read: com.xyz.product.package.B, version
          1. Read value of field y: 1.
          1. Read: object #
          1. Read: class #
          1. Read value of field y: 2.
          1. Read: object # 9. Read: null

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 37 Reading it back

  1. Read: com.xyz.product.package.A, version 1
  2. Read value of field x: 17
  3. Read: com.xyz.product.package.B, version 2
4. Read value of field y: 1.

4. Read value of field y: 1.

  1. Read: object #
  2. Read: class #
  3. Read value of field y: 2.
  4. Read: null 9. Read: null
  5. Read: object # U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 38 Reading it back
  6. Read: com.xyz.product.package.A, version 1
  7. Read value of field x: 17
  8. Read: com.xyz.product.package.B, version 2
  9. Read value of field y: 1.
  10. Read: object #
  11. Read: class #
  12. Read value of field y: 2.
  13. Read: null
  14. Read: null 10. Read: object # UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 39 How you do it: saving (serializing) FileOutputStream fout = new FileOutputStream(filename); ObjectOutputStream oout = new ObjectOutputStream(fout); oout.writeObject(snapshot); oout.close(); // if just one object U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 40 How you do it: restoring (de-serializing) FileInputStream fin = new FileInputStream(filename); ObjectInputStream oin = new ObjectInputStream(fin); GameSnapshot snapshot = (GameSnapshot)oin.readObject(); oin.close(); // if nothing more UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 41 Beyond the recipe „ Can use other kinds of streams, not just files: key is building an Object stream „ Can customize readObject and writeObject, if you need to (not common) „ Can also substitute a different object for a given one, when saving and when restoring „ Read the Java tutorial to learn these details if you ever need them U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 42 Other useful things to know „ Serialization skips transient fields „ Use that qualifier for things not to save „ Must somehow initialize on restore „ De-serialization requires a no-argument constructor that works „ Serialization requires that a class implement the Serializable marker interface „ I.e., you must designate a class to serialize it „ Need try-blocks because of possible I/O errors

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 43 About serialization for the project „ A Game will not serialize …

„ But you can make a GameSnapshot do it

„ Most Golden T types will not serialize

„ But Sprites will …

„ Except! … Their images won’t

„ So: Save a file name or other description, and

re-get the image when restoring