Mutable, Immutable, and Cloneable Objects Chapter 15.

26
Mutable, Immutable, and Cloneable Objects Chapter 15
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    231
  • download

    1

Transcript of Mutable, Immutable, and Cloneable Objects Chapter 15.

Page 1: Mutable, Immutable, and Cloneable Objects Chapter 15.

Mutable, Immutable, and Cloneable Objects

Chapter 15

Page 2: Mutable, Immutable, and Cloneable Objects Chapter 15.

2

Chapter Contents

Mutable and Immutable Objects• Companion Classes• Using Inheritance to Form Companion

Classes

Cloneable Objects

A Sorted List of Clones

Cloning an Array

Cloning a Chain

Page 3: Mutable, Immutable, and Cloneable Objects Chapter 15.

3

Mutable and Immutable Objects

A mutable object belongs to a class that has 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.

4

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.

5

Mutable and Immutable Objects

Immutable object belongs to a class that does NOT have mutator or set methodsClass said to be read onlyPlacing immutable objects in a sorted list is a way to prevent the client from destroying the order of the listUse an immutable object if it will be sharedUse a mutable object if its data will change frequently

Page 6: Mutable, Immutable, and Cloneable Objects Chapter 15.

6

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

Page 7: Mutable, Immutable, and Cloneable Objects Chapter 15.

7

Companion Classes

Fig. 15-3 the classes Name and ImmutableName

Page 8: Mutable, Immutable, and Cloneable Objects Chapter 15.

8

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 9: Mutable, Immutable, and Cloneable Objects Chapter 15.

9

Companion Classes

Inheritance can be used to form companion classes

Text shows declaration of ImmutableName

Then uses this to declare the derived class Name• Invokes protected methods of base class• Adds mutator methods

It is best to derive the mutable class from the immutable class

Page 10: Mutable, Immutable, and Cloneable Objects Chapter 15.

10

Companion Classes

Fig. 15-4 The class Name is derived from the class ImmutableName

Page 11: Mutable, Immutable, and Cloneable Objects Chapter 15.

11

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

public class MyClass implements Cloneable

{ . . .

Page 12: Mutable, Immutable, and Cloneable Objects Chapter 15.

12

Cloneable Objects

Fig. 15-5 (a) A shallow clone; (b) a deep clone.

Page 13: Mutable, Immutable, and Cloneable Objects Chapter 15.

13

Cloneable Objects

Fig. 15-6 An instance of Name and its shallow clone.

Page 14: Mutable, Immutable, and Cloneable Objects Chapter 15.

14

Cloneable Objects

Fig. 15-7 A clone after one of its data fields is changed.

Page 15: Mutable, Immutable, and Cloneable Objects Chapter 15.

15

Cloneable Objects

A clone method for class Student that does a deep copy

public Object clone(){ try

{ Student theCopy = (Student)super.clone();theCopy.fullName = (Name)fullName.clone();return theCopy;

}catch (CloneNotSupportedException e){ throw new Error(e.toString());}

} // end clone

Page 16: Mutable, Immutable, and Cloneable Objects Chapter 15.

16

Cloneable Objects

Fig. 15-8 An instance of Student and its clone, including a deep copy of fullName.

Page 17: Mutable, Immutable, and Cloneable Objects Chapter 15.

17

Cloneable Objects

Fig. 15-9 A shallow copy of fullName.

Page 18: Mutable, Immutable, and Cloneable Objects Chapter 15.

18

Tasks for a clone Method

Invoke the clone method of the superclass with super.clone()Enclose call to clone in a try blockWrite 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 possibleReturn the clone

Page 19: Mutable, Immutable, and Cloneable Objects Chapter 15.

19

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 20: Mutable, Immutable, and Cloneable Objects Chapter 15.

20

A Sorted List of Clones

Fig. 15-10 An ADT and its client after the clone of an object is added to the ADT.

Page 21: Mutable, Immutable, and Cloneable Objects Chapter 15.

21

A Sorted List of Clones

Fig. 15-11 The effect of getEntry if it did not return a clone.

Page 22: Mutable, Immutable, and Cloneable Objects Chapter 15.

22

A Sorted List of Clones

Fig. 15-12 The effect of getEntry when it does return a clone.

Page 23: Mutable, Immutable, and Cloneable Objects Chapter 15.

23

Cloning an Array

To make a deep clone of an array a of cloneable objects• The class must implement Cloneable• Invoke a.clone()• Clone each object in the array

Thing[ ] clonedArray = (Thing[ ])myArray.clone();

for (int index = 0; index < myArray.length; index++)

clonedArray[index] = (Thing)myArray[index].clone();

Page 24: Mutable, Immutable, and Cloneable Objects Chapter 15.

24

Cloning a Chain

The ADT list class must implement the interface Cloneable

public class LList implements ListInterface, Cloneable

{private Node firstNode; // reference to first nodeprivate int length; // number of entries in list

. . .

Page 25: Mutable, Immutable, and Cloneable Objects Chapter 15.

25

Cloning a Chain

Fig. 15-13 A list that stores its data in a linked chain and its shallow clone.

Page 26: Mutable, Immutable, and Cloneable Objects Chapter 15.

26

Cloning a Chain

Fig. 15-14 A list that stores its data in a linked chain and its deep clone.