Tải bản đầy đủ (.pdf) (33 trang)

Software design: Lecture 32 - Sheraz Pervaiz

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (460.42 KB, 33 trang )

1

Software Design
Lecture : 32


2

Category of Performing Cloning
Shallow Copy

Deep Copy


3

Cloned as Shallow Copy
 The  original  top­level  object  and  all  of  its  primitive 
members are duplicated.

  Any lower­level objects that the top­level 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 lower­level object.


4



5

Cloning as Deep Object
 The  original  top­level  object  and  all  of  its  primitive 
members are duplicated.

  Any lower­level objects that the top­level object contains 
are also duplicated.

 In this case, both the original and the cloned object refer 
to two different lower­level 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; //Lower­level 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("Person­A" ,"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("Person­B"); //change the primitive member
q.getCar().setName("Accord"); //change the lower­level 
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): Person­A
Civic
Clone (before change): Person­A ­ Civic

Clone (after change): Person­B ­ Accord
Original (after clone is modified): Person­A ­ 
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 {
//Lower­level 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("Person­A", "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("Person­B");
//change the lower­level 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): Person­A
Civic
Clone (before change): Person­A ­ Civic

Clone (after change): Person­B ­ Accord
Original (after clone is modified): Person­A ­ 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


×