Two Ways to Realize Deep Copy in Java clone of Serialized

  • 2021-07-07 07:26:10
  • OfStack

The clone () method is a bit more cumbersome, requiring all the classes involved to implement the declarative interface Cloneable, override the clone () method in the Object class, and set the scope to public (so that the clone method can be used by other classes).

The serialization method is simple, and it is necessary to implement the interface Serializable for all involved classes


package b1ch06.clone;

import java.io.Serializable;

class Car implements Cloneable, Serializable {
  private String band;

  public Car(String band) {
    this.band = band;
  }

  public String getBand() {
    return band;
  }

  public void setBand(String band) {
    this.band = band;
  }

  @Override
  public Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
}

package b1ch06.clone;

import java.io.Serializable;

class Employee implements Cloneable, Serializable {
  private String name;
  private Car car;

  public Employee(String name, Car car) {
    this.name = name;
    this.car = car;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Car getcar() {
    return car;
  }

  public void setcar(Car car) {
    this.car = car;
  }

  protected void test() {
    System.out.println("test func");
  }

  @Override
  public Object clone() throws CloneNotSupportedException {

    Employee employee_cloned = (Employee) super.clone();
    Car car_cloned = (Car) this.car.clone();
    employee_cloned.setcar(car_cloned);
    return employee_cloned;
  }
}



package b1ch06.clone;


import java.io.*;


public class SerializedClone {
  @SuppressWarnings("unchecked")
  public static <T extends Serializable> T clone(T obj) {
    T cloneObj = null;
    try {
      // Write byte stream 
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      ObjectOutputStream obs = new ObjectOutputStream(out);
      obs.writeObject(obj);
      obs.close();

      // Allocate memory, write original objects, and generate new objects 
      ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
      ObjectInputStream ois = new ObjectInputStream(ios);
      // Returns the generated new object 
      cloneObj = (T) ois.readObject();
      ois.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return cloneObj;
  }


}

package b1ch06.clone;

public class MyClone {


  public static void main(String[] args) {
    Car car = new Car("BMW");
    Employee employee = new Employee("ANDY", car);
    //  Method 1 Object that overrides all the classes involved clone() Method 
    try {

      Employee employee_cp = (Employee) employee.clone();

      System.out.println("=========================");
      System.out.println("original Object address ?:");
      System.out.println(employee.toString());
      System.out.println("copy Object address ?:");
      System.out.println(employee_cp.toString());
      System.out.println(" The front and back objects point to the same 1 Address ?:");
      System.out.println(employee_cp == employee);
      System.out.println("=========================");

      System.out.println("original Object car Object address ?:");
      System.out.println(employee.getcar().toString());
      System.out.println("copy Object car Object address ?:");
      System.out.println(employee_cp.getcar().toString());
      System.out.println(" Two before and after car Object points to the same 1 Address ?:");
      System.out.println(employee_cp == employee);

    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
    }

    //  Method 2 Serialization for deep copy 
    Employee cloned_employee = SerializedClone.clone(employee);
    System.out.println("=========================");
    System.out.println("original Object address ?:");
    System.out.println(employee.toString());
    System.out.println("copy Object address ?:");
    System.out.println(cloned_employee.toString());
    System.out.println(" The front and back objects point to the same 1 Address ?:");
    System.out.println(cloned_employee == employee);

    System.out.println("=========================");

    System.out.println("original Object car Object address ?:");
    System.out.println(employee.getcar().toString());
    System.out.println("copy Object car Object address ?:");
    System.out.println(cloned_employee.getcar().toString());
    System.out.println(" Two before and after car Object points to the same 1 Address ?:");
    System.out.println(cloned_employee == employee);

  }
}

Related articles: