Mutable Immutable and Cloneable Objects Chapter 15 Slides
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong Le. Tourneau University Longview, TX ã 2007, Prentice Hall
Chapter Contents • Mutable and Immutable Objects § Creating a Read-Only Class § Companion Classes • Cloneable Objects § Cloning an Array § Cloning a Chain § A Sorted List of Clones Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Mutable and Immutable Objects • A mutable object belongs to a class that has public mutator or set methods for its data fields • The client uses set methods to change values of the object's data fields Fig. 15 -1 An object and its reference variable chris Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Mutable and Immutable Objects Done by executing chris. set. Last ("Smith"); Fig. 15 -2 An object in the list name. List (a) initially; (b) after the reference variable chris is used to change it Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Mutable and Immutable Objects • Immutable object belongs to a class that does NOT have mutator or set methods • Class said to be read only • Placing immutable objects in a sorted list is a way to prevent the client from destroying the order of the list • Use an immutable object if it will be shared • Use a mutable object if its data will change frequently Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Creating a Read-Only Class • Recall class Name from Chapter 1 § We wish a version of this class to be read-only § View result, class Immutable. Name • Design guidelines for read-only class § § Should be final Data fields private No public set methods Data fields that are mutable objects should be final Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Companion Classes • If it is necessary to alter an immutable object § Can be accomplished by having a companion class of corresponding mutable objects • Also helpful to have methods that convert an object from one type to another • Note additional methods for Immutable. Name to allow cross conversion Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Companion Classes Fig. 15 -3 the classes Name and Immutable. Name Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Companion Classes • Java's String class is a read-only class § Instances of String are immutable • Java provides a companion class, String. Buffer § Has a constructor that takes an instance of String as an argument § Has the to. String method that converts a mutable instance of String. Buffer to an immutable instance of String Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloneable Objects • A clone is a copy of an object • The Object class contains a protected method clone that returns a copy of an object § The implementation of any method can invoke clone § Clients cannot invoke clone unless the class overrides it, declares it public Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloneable Objects Fig. 15 -4 (a) A shallow clone; (b) a deep clone. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloneable Objects Fig. 15 -5 An instance of Name and its shallow clone. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloneable Objects Fig. 15 -6 A clone after one of its data fields is changed. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloneable Objects • Recall class Student from Chapter 2 • Consider a clone method for the class Student § View source code • Note § Name has set methods, full. Name is mutable § Must clone full. Name within Student's clone method Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloneable Objects Fig. 15 -7 An instance of Student and its clone, including a deep copy of full. Name. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloneable Objects Fig. 15 -8 A shallow copy of full. Name. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Tasks for a clone Method • Invoke the clone method of the superclass with super. clone() • Enclose call to clone in a try block • Write a catch block to handle exception of Clone. Not. Supported. Exception § Skip if super. clone() invokes a public clone method • Clone mutable data fields of object super. clone() returned, when possible • Return the clone Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloning an Array • Define a new interface § Declares public method clone § Overrides Object's protected Copyable method • View full implementation of clone Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloning a Chain • The ADT list class must implement the interface Cloneable Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloning a Chain Fig. 15 -9 A list and its shallow clone: linked implementation Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Cloning a Chain Fig. 15 -10 A list and its deep clone: linked implementation Click to view complete clone method for a chain Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
A Sorted List of Clones • Recall problem of placing mutable objects in an ADT (such as a sorted list) • If object is cloned before it is added to an ADT § Client could access/change the ADT's data only by using ADT operations § Requires that object added to the ADT be Cloneable Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
A Sorted List of Clones Fig. 15 -11 An ADT and its client after the clone of an object is added to the ADT. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
A Sorted List of Clones Fig. 15 -12 The effect of get. Entry if it did not return a clone. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
A Sorted List of Clones Fig. 15 -12 The effect of get. Entry when it does return a clone. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
- Slides: 25