CMPSCI 220 Programming Methodology Spring 2013 ... - Edlab

farflungconvyancerΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 3 χρόνια και 11 μήνες)

108 εμφανίσεις

1
CMPSCI 220
Programming Methodology
Spring 2013
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
12 – The Flyweight Pattern and
Java Serialization
Spring

2013
Assignments, etc.

HW 2B – Due March 6
th
(Wednesday)

HW 3A – Assigned March 6
th
, due March 13
th

T
OMORRO
W
: “
W
dro
p
date”
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
W W p

Final exam: Now on schedule:

Friday May 3
rd
, ELab II 119, 8-10am

Similar in style to the midterm
2
Learning Objectives

Students will understand and be able to
recognize and apply the Flyweight Pattern

Students will understand
J
ava serialization
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
3
J
and be aware of its intricacies
Flyweight Pattern

Use when one instance of a class can serve as many
“virtual instances”

Reduces number of object instances, saves space

At run time
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
4

In stable storage (database, file system, etc.)

“Centralizes state for many ‘virtual’ objects”

HFDP means this one way

Alternative interpretation probably more common

“Used when a class has many instances, and they can
all be controlled identically” - HFDP

Again, a broader interpretation probably better
HFDP’s Flyweight Example
Tree
xCoord
yCoord
age
TreeManager
treeArray
displayTrees() {
//for all trees {
Tree
display(x, y, age) {
//use X-Y coords
//& complex age
-
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
5
age
display() {
//use X-Y coords
//& complex age-
//related calcs
}
//for

all

trees

{
//get array row
display(x, y, age);
}
}
//&

complex

age
//related calcs
}

One single,
state-free
Tree object
• State for all virtual
Tree objects stored
in treeArray
• Each Tree instance
maintains its own state
HFDP’s Flyweight Example:
Storing information “by column”
TreeManager
xcoordArray
ycoordArray
a
g
eArra
y
Tree
display(x, y, age) {
//use X-Y coords
//
& com
p
lex a
g
e-
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
6
g y
displayTrees() {
//for all trees {
//get array row
display(x, y, age);
}
}
//p g
//related calcs
}
Saves space compared with separate objects, but
places more management burden on the programmer
2
More Typical Flyweight Example
Tree
xCoord
yCoord
age
Tree
xCoord
yCoord
age
TreeInfo
//large amount
//of data common
//to all (or some)
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
7
age
//large amount
//of data common
//to all (or some)
//trees
age
myInfo
//to

all

(or

some)
//trees

One single,
immutable
TreeInfo
object
• Each Tree instance
maintains its own copy
of common data
• Each Tree instance
points to one shared
copy of common data
Flyweight in the Project

Blocks have x, y position, width, height, and color

In a given game many blocks have the same width,
height, and color

䍡C g牯異 瑨慴 楮景牭慴楯i i湴n a 獨慲敤
block
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
8

Can

group

that

information

into

a

shared

block

description object

Individual blocks then indicate only their position and
description

This is something we will implement in HW 3B
Recall: The Memento Pattern

Goals:

Save important state of a system’s key object …

While maintaining the key object’s encapsulation
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
9

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
Memento Pattern
Client
//when important state is reached
Object saved =
(Object)ko.getCurrentState();
KeyObjectMemento
savedObjectState
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
10
//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
Memento Pattern in the Project
BreakoutGame
Memento snapshot;
//when save is requested
snapshot = game memento();
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
11
snapshot

=

game
.
memento();
//when restoring (a new
game)
… = new BreakoutGame(gameEngine, snapshot); //
Memento memento() { return new Memento(… gathered state …); }
BreakoutGame(BOEngine engine, Memento saved) { … snapshot = saved; … }
BreakoutGame(BOEngine engine) { … snapshot = null; … }
In-memory or saved?

Cloning objects just gives an in-memory copy

Fine for undoing or returning to a recent state

䙩湥 楦 睥 don
’
瑣rash
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
12

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
3
Java Serialization

Goal:To save a connected group of objects
to a
stream, so that we can restore them, with their same
connections
,later.
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
13

Serialize = to turn into a series
of bytes

Why is this tricky?
Java Serialization

Goal:To save a connected group of objects
to a
stream, so that we can restore them, with their same
connections
,later.
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
14

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
Rough procedure at each object

Write information about the object’s class

For an array, write its number of elements
Write each of the object

s
fields
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
15

Write

each

of

the

object

s

fields

Primitives: convert to bytes (about 4-8 bytes)

Objects: Serialize the referent

If the referent has already been processed, note
which object it was
What about sharing and cycles?

The objects written are numbered:1, 2, 3, …

The first
time we write an object we proceed
as normal
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
16

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.)
A fine point

The classes mentioned are also numbered

The first
time a class is mentioned, we write:

䥴I f畬u n慭a s畣u as j慶a u瑩t A牲慹䱩獴
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
17

Its

full

name
,
such

as

java
.
util
.
ArrayList

Its version number, which allows for evolution

After the first mention, we just write a special
entry, “previous class number n”
A small example
Want to serialize starting with the object of class A.
This should save that object, and the two of class B.
It should save their primitive fields, and all the
connections between them.
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
18
connections

between

them.
This should happen in such a way that we can
restore them later.
4
A small example
1.
Write: com.xyz.product.package.A, version 1
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
19
A small example
1.
Write: com.xyz.product.package.A, version 1
2.
Write value of field x: 17
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
20
A small example
1.
Write: com.xyz.product.package.A, version 1
2.
Write value of field x: 17
3.
Write: com.xyz.product.package.B, version 2
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
21
A small example
1.
Write: com.xyz.product.package.A, version 1
2.
Write value of field x: 17
3.
Write: com.xyz.product.package.B, version 2
4
Write value of field y:1 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
22
4
.
Write

value

of

field

y:

1
.
5
A small example
1.
Write: com.xyz.product.package.A, version 1
2.
Write value of field x: 17
3.
Write: com.xyz.product.package.B, version 2
4
Write value of field y:1 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
23
4
.
Write

value

of

field

y:

1
.
5
5.
Write: object #1
A small example
1.
Write: com.xyz.product.package.A, version 1
2.
Write value of field x: 17
3.
Write: com.xyz.product.package.B, version 2
4
Write value of field y:1 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
24
4
.
Write

value

of

field

y:

1
.
5
5.
Write: object #1
6.
Write: class #2
5
A small example
1.
Write: com.xyz.product.package.A, version 1
2.
Write value of field x: 17
3.
Write: com.xyz.product.package.B, version 2
4
Write value of field y:1 5
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
25
4
.
Write

value

of

field

y:

1
.
5
5.
Write: object #1
6.
Write: class #2
7.
Write value of field y: 2.3
A small example
1.
Write: com.xyz.product.package.A, version 1
2.
Write value of field x: 17
3.
Write: com.xyz.product.package.B, version 2
4
Write value of field y:1 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
26
4
.
Write

value

of

field

y:

1
.
5
5.
Write: object #1
6.
Write: class #2
7.
Write value of field y: 2.3
8.
Write: null
A small example
1.
Write: com.xyz.product.package.A, version 1
2.
Write value of field x: 17
3.
Write: com.xyz.product.package.B, version 2
4
Write value of field y:1 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
27
4
.
Write

value

of

field

y:

1
.
5
5.
Write: object #1
6.
Write: class #2
7.
Write value of field y: 2.3
8.
Write: null
9.
Write: null
A small example
1.
Write: com.xyz.product.package.A, version 1
2.
Write value of field x: 17
3.
Write: com.xyz.product.package.B, version 2
4
Write value of field y:1 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
28
4
.
Write

value

of

field

y:

1
.
5
5.
Write: object #1
6.
Write: class #2
7.
Write value of field y: 2.3
8.
Write: null
9.
Write: null
10.
Write: object #3
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 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
29
4
.
Read

value

of

field

y:

1
.
5
5.
Read: object #1
6.
Read: class #2
7.
Read value of field y: 2.3
8.
Read: null
9.
Read: null
10.
Read: object #3
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 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
30
4
.
Read

value

of

field

y:

1
.
5
5.
Read: object #1
6.
Read: class #2
7.
Read value of field y: 2.3
8.
Read: null
9.
Read: null
10.
Read: object #3
6
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 5
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
31
4
.
Read

value

of

field

y:

1
.
5
5.
Read: object #1
6.
Read: class #2
7.
Read value of field y: 2.3
8.
Read: null
9.
Read: null
10.
Read: object #3
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 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
32
4
.
Read

value

of

field

y:

1
.
5
5.
Read: object #1
6.
Read: class #2
7.
Read value of field y: 2.3
8.
Read: null
9.
Read: null
10.
Read: object #3
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 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
33
4
.
Read

value

of

field

y:

1
.
5
5.
Read: object #1
6.
Read: class #2
7.
Read value of field y: 2.3
8.
Read: null
9.
Read: null
10.
Read: object #3
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 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
34
4
.
Read

value

of

field

y:

1
.
5
5.
Read: object #1
6.
Read: class #2
7.
Read value of field y: 2.3
8.
Read: null
9.
Read: null
10.
Read: object #3
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 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
35
4
.
Read

value

of

field

y:

1
.
5
5.
Read: object #1
6.
Read: class #2
7.
Read value of field y: 2.3
8.
Read: null
9.
Read: null
10.
Read: object #3
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 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
36
4
.
Read

value

of

field

y:

1
.
5
5.
Read: object #1
6.
Read: class #2
7.
Read value of field y: 2.3
8.
Read: null
9.
Read: null
10.
Read: object #3
7
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 5
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
37
4
.
Read

value

of

field

y:

1
.
5
5.
Read: object #1
6.
Read: class #2
7.
Read value of field y: 2.3
8.
Read: null
9.
Read: null
10.
Read: object #3
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 5
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
38
4
.
Read

value

of

field

y:

1
.
5
5.
Read: object #1
6.
Read: class #2
7.
Read value of field y: 2.3
8.
Read: null
9.
Read: null
10.
Read: object #3
How you do it: saving (serializing)
FileOutputStream fout =
new FileOutputStream(filename);
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
39
ObjectOutputStream oout =
new ObjectOutputStream(fout);
oout.writeObject(snapshot);
oout.close(); // if no more groups
// of objects to write
How you do it: restoring
(de-serializing)
FileInputStream fin =
new FileInputStream(filename);
ObjectInputStream oin =
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
40
ObjectInputStream

oin

=
new ObjectInputStream(fin);
GameSnapshot snapshot =
(GameSnapshot)oin.readObject();
oin.close(); // if no more groups of
// objects to read in
Beyond the recipe

Can use other kinds of streams, not just files:
key is building an
Object
stream on top of
some other stream
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
41

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
Other useful things to know

Serialization skips
transient
fields

Use that qualifier for things not to save

Must somehow initialize on restore
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
42

De-serialization requires a no-argument constructor
that works

Serialization requires that a class implement the
Serializable
marker interface

I.e., you must mark a class to be able to serialize it

Need try-catch blocks because of possible I/O errors
8
About serialization for the project

A Game will not serialize …

But you can make a
GameSnapshot
do it

䵯獴 䝯汤敮 T 瑹灥t 睩汬
not
serialize
U
U
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science
43

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

We will do this in Homework 3B
Effective Java Items

74: Implement Serialization judiciously

75: Consider using a custom serialized form
76 Write readObject methods defensi el
UU
NIVERSITY OF
NIVERSITY OF
M
M
ASSACHUSETTS
ASSACHUSETTS
, A
, A
MHERST •
MHERST •
Department of Computer Science
Department of Computer Science

76
:
Write

readObject

methods

defensi
v
ely

77: For instance control, prefer enum types
to readResolve

78: Consider serialization proxies to serialized
instances

Essentially what serializing a memento does!
44