




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Programming Methodology; Subject: Computer Science; University: University of Massachusetts - Amherst; Term: Spring 2009;
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science
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:
Benefits:
Considerations:
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 3 Memento Pattern
U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science 4 Memento Pattern in the Project
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
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
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
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) }
UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science 37 Reading it back
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 …
Most Golden T types will not serialize