Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSC 243 – Java Programming, Spring 2014 March, 2014 Week 8ish, java.lang.Clonable & java.io.Serializable Interfaces java.lang.Cloneable • Cloning is akin to a copy constructor in C++. • java.lang.Object defines this method • protected Object clone() throws CloneNotSupportedException • java.lang.Cloneable is a marker interface • It does not define any method. • A class that inherits Cloneable guarantees that it provides a public clone() method that returns a copy of the object on which clone() is invoked. Shallow versus deep copy • Shallow copy clones the elements of a container by copying references to contained objects. It does not clone contained objects. • LinkedList<String> dictionary = LIST WITH ELEMENTS • LinkedList<String> listcopy = dictionary.clone(); • Deep copy clones the elements, recursively. • LinkedList<IFillWord> original = SOME ELEMENTS • LinkedList<IFillWord> duplicate = new LinkedList<IFillWord> (); • for (IFillWord game : original) { » duplicate.add(game.clone()); • } Covariance of subclass & return type • Interfaces and classes that subclass Cloneable can refine its return type to a subclass of the base class clone() method, and loosen protection to public. • Object has protected Object clone() • IFillWord has public IFillWord clone() • FillWordBasic has public FillWordBasic clone() • FillWordGrows has public FillWordGrows clone() • Internally clone() constructs a new object and copies fields from the original object using shallow or deep copy. Our classes use deep copy on the board and random number generator. java.io.Serializable • Serializable is also a marker interface. • It guarantees that its object fields consist of primitive types, Serializable objects, and arrays of these two types. • Serialization ignores fields marked as transient. • A Serializable object easily converts to and from a byte stream (a byte []) for transmission over a network or storage in a file. • There are special methods you can define for Serializable objects with some non-Serializable fields. We will ignore these for now. Serializable Object graphs • When multiple objects contain fields that point to each other, that is an object graph. • Java input-output classes have automatic means for serializing an entire object graph, as long as it consists strictly of Serializable objects, primitive types, and arrays of these. • Transmitting or storing object graphs is almost easy. • Field private static final long serialVersionUID = A_LONG_VALUE ; is used to tag the version number of a Serialized object, in case a later version of its class changes field types. FillWordHelper fields in Assignment 3 protected transient List<String> dictionary protected char [][] board protected int wordSizeLimit protected RandomCloneable randomNumGenerator protected boolean isacross protected String wordToPlay protected int score protected int swaps We will clone() using deep copy on randomNumGenerator & board. Random is Serializable. I have subclassed it in RandomCloneable to make randomNumGenerator Cloneable. We will serialize every field except dictionary. We do not want to store or transmit a copy of the dictionary on every step. Applications for last year • We usedserialization & deserialization to save & restore games in files in Assignment 3. • We could use clone() to maintain an undo – redo stack of copies of games. • We have used clone() in a past grad O-O course to allow artificial software players (AIs) to play thousands of games in searching for best moves by cloning ScrabbleGame state.