Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."— Presentation transcript:

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

2 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

3 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

4 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");

5 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

6 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 1class Name  We wish a version of this class to be read- only  View result, class ImmutableNameclass 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

7 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 conversionadditional methods

8 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

9 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

10 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

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

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

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

14 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 2class Student Consider a clone method for the class Student  View source codesource code Note  Name has set methods, fullName is mutable  Must clone fullName within Student 's clone method

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

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

17 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

18 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 clonefull implementation

19 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

20 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

21 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

22 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

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

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

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


Download ppt "Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."

Similar presentations


Ads by Google