1
Software Design
Lecture : 32
2
Category of Performing Cloning
Shallow Copy
Deep Copy
3
Cloned as Shallow Copy
The original toplevel object and all of its primitive
members are duplicated.
Any lowerlevel objects that the toplevel object contains
are not duplicated.
Only references to these objects are copied. This results
in both the original and the cloned object referring to the
same copy of the lowerlevel object.
4
5
Cloning as Deep Object
The original toplevel object and all of its primitive
members are duplicated.
Any lowerlevel objects that the toplevel object contains
are also duplicated.
In this case, both the original and the cloned object refer
to two different lowerlevel objects
6
7
Java Code of Shallow Vs Deep Class
8
<<I>>
Cloneable
Person
Object 1 of
Person
Car
Object 2 of
Person
9
Shallow Copy
10
package ShallowCopy;
class Car {
private String name;
public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
public Car(String s) {
name = s;
}
}
11
class Person implements Cloneable {
private Car car; //Lowerlevel object
private String name;
public Car getCar() {
return car;
}
public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
public Person(String s, String t) {
name = s;
car = new Car(t);
}
12
public Object clone() {
try {//shallow copy
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}}}
13
public class ShallowTestCopy
{
public static void main(String[] args)
{
Person p = new Person("PersonA" ,"Civic"); //Original Object
System.out.println("Original (orginal values): " + p.getName());
System.out.println(p.getCar().getName());
Person q = (Person) p.clone(); //Clone as a shallow copy
System.out.println("Clone (before change): " + q.getName() + "
" + q.getCar().getName());
14
q.setName("PersonB"); //change the primitive member
q.getCar().setName("Accord"); //change the lowerlevel
object
System.out.println("Clone (after change): " + q.getName()
+ " " + q.getCar().getName());
System.out.println("Original (after clone is modified): " +
p.getName() + " " + p.getCar().getName());
} }
15
OUTPUT
Original (orginal values): PersonA
Civic
Clone (before change): PersonA Civic
Clone (after change): PersonB Accord
Original (after clone is modified): PersonA
Accord
16
Deep Copy
17
class Car {
private String name;
public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
public Car(String s) {
name = s;
}
}
18
class Person implements Cloneable {
//Lowerlevel object
private Car car;
private String name;
public Car getCar() {
return car;
}
public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
public Person(String s, String t) {
name = s;
car = new Car(t);
}
19
public Object clone() {
//Deep copy
Person p = new Person(name, car.getName());
return p;
}
}
20
public class DeepTestCopy
{
public static void main(String[] args) {
//Original Object
Person p = new Person("PersonA", "Civic");
System.out.println("Original (orginal values): " +p.getName());
System.out.println(p.getCar().getName());
Person q = (Person) p.clone(); //Clone as a shallow copy
System.out.println("Clone (before change): " + q.getName() + "
" + q.getCar().getName());
21
//change the primitive member
q.setName("PersonB");
//change the lowerlevel object
q.getCar().setName("Accord");
System.out.println("Clone (after change): " +q.getName()
+ " " +q.getCar().getName());
System.out.println("Original (after clone is modified): " +
p.getName() + " " + p.getCar().getName());
}}
22
OUTPUT
Original (orginal values): PersonA
Civic
Clone (before change): PersonA Civic
Clone (after change): PersonB Accord
Original (after clone is modified): PersonA Civic
23
Problem Statement
24
A computer user in a typical organization is associated with a
user account. A user account can be part of one or more
groups. Permissions on different resources (such as servers,
printers, etc.) are defined at the group level. A user gets all the
permissions defined for all groups that his or her account is
part of.
Let us build an application to facilitate the creation of user
accounts.
For simplicity, let us consider only two groups — Supervisor
and AccountRep — representing users who are supervisors
and account representatives, respectively and we have defined
permissions in text files for each user group.
25