Monday, June 4, 2007

Object.clone();

Para entender como funciona a clonagem de objetos em Java fiz este exemplo, basta analizar o codigo e a execuçao, e tirar as proprias conclusões :P

[CODE]

public class RunVehiclesClone {

public static void main(String[] args) {
System.out.println();
System.out.println("===================================");
System.out.println();
run();
System.out.println("===================================");
System.out.println();
runClone();
System.out.println("===================================");
System.out.println();
runCloneDeep();
System.out.println("===================================");
System.out.println();
runCloneSerialization();
System.out.println("===================================");
System.out.println();
}

private static void run() {
System.out.println("-----------------------------------");
System.out.println("NORMAL");
System.out.println("-----------------------------------");
java.util.List<Vehicle> listVehicles = new java.util.ArrayList();

Vehicle vehicle = new Vehicle();
vehicle.setName("Vehicle");

VehicleDescription vehicleDescription = new VehicleDescription();
vehicleDescription.setName("Description");

vehicle.setVehicleDescription(vehicleDescription);

listVehicles.add(vehicle);

Vehicle superBike = vehicle;
superBike.setName("SuperBike");
superBike.getVehicleDescription().setName("Kawasaki Ninja ZXR6");

listVehicles.add(superBike);

Vehicle car = vehicle;
car.setName("Car");
car.getVehicleDescription().setName("Audi A3");

listVehicles.add(car);

System.out.println();
for (Vehicle _vehicle : listVehicles) {
System.out.println(_vehicle);
System.out.println();
}
}

private static void runClone() {
System.out.println("-----------------------------------");
System.out.println("CLONE");
System.out.println("-----------------------------------");
java.util.List<Vehicle> listVehicles = new java.util.ArrayList();

Vehicle vehicle = new Vehicle();
vehicle.setName("Vehicle");

VehicleDescription vehicleDescription = new VehicleDescription();
vehicleDescription.setName("Description");

vehicle.setVehicleDescription(vehicleDescription);

listVehicles.add(vehicle);

Vehicle superBike = (Vehicle)vehicle.clone();
superBike.setName("SuperBike");
superBike.getVehicleDescription().setName("Kawasaki Ninja ZXR6");

listVehicles.add(superBike);

Vehicle car = (Vehicle)vehicle.clone();
car.setName("Car");
car.getVehicleDescription().setName("Audi A3");

listVehicles.add(car);

System.out.println();
for (Vehicle _vehicle : listVehicles) {
System.out.println(_vehicle);
System.out.println();
}
}

private static void runCloneDeep() {
System.out.println("-----------------------------------");
System.out.println("CLONE > DEEP");
System.out.println("-----------------------------------");
java.util.List<Vehicle> listVehicles = new java.util.ArrayList();

Vehicle vehicle = new Vehicle();
vehicle.setName("Vehicle");

VehicleDescription vehicleDescription = new VehicleDescription();
vehicleDescription.setName("Description");

vehicle.setVehicleDescription(vehicleDescription);

listVehicles.add(vehicle);

Vehicle superBike = (Vehicle)vehicle.cloneDeep();
superBike.setName("SuperBike");
superBike.getVehicleDescription().setName("Kawasaki Ninja ZXR6");

listVehicles.add(superBike);

Vehicle car = (Vehicle)vehicle.cloneDeep();
car.setName("Car");
car.getVehicleDescription().setName("Audi A3");

listVehicles.add(car);

System.out.println();
for (Vehicle _vehicle : listVehicles) {
System.out.println(_vehicle);
System.out.println();
}
}

private static void runCloneSerialization() {
System.out.println("-----------------------------------");
System.out.println("CLONE > SERIALIZATION");
System.out.println("-----------------------------------");
java.util.List<Vehicle> listVehicles = new java.util.ArrayList();

Vehicle vehicle = new Vehicle();
vehicle.setName("Vehicle");

VehicleDescription vehicleDescription = new VehicleDescription();
vehicleDescription.setName("Description");

vehicle.setVehicleDescription(vehicleDescription);

listVehicles.add(vehicle);

Vehicle superBike = (Vehicle)vehicle.cloneSerialization();
superBike.setName("SuperBike");
superBike.getVehicleDescription().setName("Kawasaki Ninja ZXR6");

listVehicles.add(superBike);

Vehicle car = (Vehicle)vehicle.cloneSerialization();
car.setName("Car");
car.getVehicleDescription().setName("Audi A3");

listVehicles.add(car);

System.out.println();
for (Vehicle _vehicle : listVehicles) {
System.out.println(_vehicle);
System.out.println();
}
}
}

//////////////////////////////////////////////

class Vehicle implements java.io.Serializable, Cloneable {
private String name = "";
private VehicleDescription vehicleDescription = null;

public Vehicle() {

}

public void setName(String _name) {
name = _name;
}

public String getName() {
return name;
}

public void setVehicleDescription(VehicleDescription _vehicleDescription) {
vehicleDescription = _vehicleDescription;
}

public VehicleDescription getVehicleDescription() {
return vehicleDescription;
}

public Object clone() {
try {
return super.clone();
} catch(CloneNotSupportedException e) {
throw new Error ("Cannot clone this object.");
}
}

public Object cloneDeep() {
try {
Vehicle vehicle = (Vehicle)super.clone();
VehicleDescription _vehicleDescription = new VehicleDescription();
_vehicleDescription.setName(getVehicleDescription().getName());
vehicle.setVehicleDescription(_vehicleDescription);
return vehicle;
} catch(CloneNotSupportedException e) {
throw new Error ("Cannot clone this object.");
}
}

public Object cloneSerialization() {
java.io.ObjectOutputStream oos = null;
java.io.ObjectInputStream ois = null;
try {
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
oos = new java.io.ObjectOutputStream(bos);
oos.writeObject(this);
oos.flush();
java.io.ByteArrayInputStream bin = new java.io.ByteArrayInputStream(bos.toByteArray());
ois = new java.io.ObjectInputStream(bin);
return ois.readObject();
} catch(Exception e) {
throw new Error ("Cannot clone this object.");
} finally {
try {
oos.close();
ois.close();
} catch(Exception e) {
}
}
}

public String toString() {
return name + " - " + vehicleDescription;
}
}

//////////////////////////////////////////////

class VehicleDescription implements java.io.Serializable {
String name = "";
java.sql.Connection con = null;
public VehicleDescription() {

}

public void setName(String _name) {
name = _name;
}

public String getName() {
return name;
}

public String toString() {
return name;
}
}

[/CODE]

No comments:

Post a Comment