Docsity

Memento Pattern and Java Serialization: Saving and Restoring State in Computer Science, Study notes of Computer Science

Partial preview of the text

Download Memento Pattern and Java Serialization: Saving and Restoring State in Computer Science and more Study notes Computer Science in PDF only on Docsity!

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

14 – The Memento Pattern; Java Serialization

CMPSCI 220 (291A)

Programming Methodology

Spring 2009

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

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

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Memento Pattern in the Project

BreakoutGame/BOEngine //when save is requestedGameSnapshot snapshot =

GameSnapshot.makeSnapshot(

(game, gameState); // **

//when restoring (a new game)… = new BreakoutGame(

gameEngine, snapshot); // ***

BreakoutGame/GameState GameState snapshotvoid saveGame() {

//gather state: code at **

} BreakoutGame(Game Engine e,

GameSnapshot s); {

//restore state: code at ***

} // do other Key Object stuff

GameSnapshot savedBallsavedBlocks…

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

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 inorder to clone an object

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Shallow versus Deep Copies

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Shallow versus Deep Copies

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

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 ofthe Memento object’s state

You will also need to clone the GameState

You need to mark these Cloneable

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

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

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

In-memory or saved?

Cloning objects just gives an in-memory copy

Fine for undoing or returning to a recent state

Fine if we don’t crash

With additional effort, we can save to a file

Called

serializing

the memento object

We now look at Java serialization in more detail

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

Java Serialization

„

Goal:

To save a connected group of objects to a

stream, so that we can restore them,

with their same

connections,

later.

„

Serialize = to turn into a series of bytes

„

Why is this tricky?

Shared

objects in the structure

Cycles

of pointers in the structure

Might be read back into a

different program

„

Could have different version of some of the classes

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

What about sharing and cycles?

The objects written are

numbered:

The

first

time we write an object we proceed

as normal

If we see other references to object number k

, we write a special entry meaning “I refer to

previous object

k

This works for both sharing

and

cycles.

(There is also a special value for

null

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

A fine point

The

classes

mentioned are also numbered

The

first

time a class is mentioned, we write:

Its full name, such as java.util.ArrayList

Its

version number

, which allows for evolution

After the first mention, we just write a specialentry, “previous class number

n

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

A small example

Write: com.xyz.product.package.A, version 1

U

U

NIVERSITY OFNIVERSITY OF

M

M

ASSACHUSETTS ASSACHUSETTS

, A

, A

MHERST MHERST •

Department of Computer Science Department of Computer Science

A small example

Write: com.xyz.product.package.A, version 1

Write value of field x: 17