mutable, immutable, and cloneable objects chapter 15 slides by steve armstrong letourneau university...

25
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall

Post on 20-Jan-2016

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Mutable, Immutable, and

Cloneable Objects

Chapter 15

Slides by Steve ArmstrongLeTourneau University

Longview, TX2007,Prentice Hall

Page 2: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

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

Page 3: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Page 4: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Fig. 15-2 An object in the list nameList (a) initially; (b) after the reference variable chris is used to change it

Done by executing chris.setLast ("Smith");

Done by executing chris.setLast ("Smith");

Page 5: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Page 6: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 ImmutableName

• 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

Page 7: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 ImmutableName to allow cross conversion

Page 8: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 ImmutableName

Page 9: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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, StringBuffer Has a constructor that takes an instance of String as an argument

Has the toString method that converts a mutable instance of StringBuffer to an immutable instance of String

Page 10: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Page 11: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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.

Page 12: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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.

Page 13: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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.

Page 14: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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, fullName is

mutable Must clone fullName within Student's clone method

Page 15: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 fullName.

Page 16: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 fullName.

Page 17: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 CloneNotSupportedException Skip if super.clone() invokes a public clone method

• Clone mutable data fields of object super.clone() returned, when possible

• Return the clone

Page 18: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Page 19: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Page 20: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Page 21: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Click to view complete clone

method for a chain

Page 22: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Page 23: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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.

Page 24: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 getEntry if it did not return a clone.

Page 25: Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 getEntry when it does return a clone.