diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowvsdeepcopy/Address.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowvsdeepcopy/Address.java new file mode 100644 index 0000000000..a31e8d7e2d --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowvsdeepcopy/Address.java @@ -0,0 +1,58 @@ +package com.baeldung.shallowvsdeepcopy; + +class Address implements Cloneable{ + + private String streetName; + private String zipCode; + private String cityName; + private String country; + + public Address(String streetName, String zipCode, String cityName, String country) { + this.streetName = streetName; + this.zipCode = zipCode; + this.cityName = cityName; + this.country = country; + } + + public String getStreetName() { + return streetName; + } + + public void setStreetName(String streetName) { + this.streetName = streetName; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + @Override + public Address clone() { + try { + Address clone = (Address) super.clone(); + return clone; + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowvsdeepcopy/UserWithDeepClone.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowvsdeepcopy/UserWithDeepClone.java new file mode 100644 index 0000000000..70646d396c --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowvsdeepcopy/UserWithDeepClone.java @@ -0,0 +1,49 @@ +package com.baeldung.shallowvsdeepcopy; + +public class UserWithDeepClone implements Cloneable{ + + private String name; + private Address address; + private int age; + + public UserWithDeepClone(String name, Address address, int age) { + this.name = name; + this.address = address; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public UserWithDeepClone clone() { + try { + UserWithDeepClone clone = (UserWithDeepClone) super.clone(); + clone.setAddress(clone.getAddress().clone()); + return clone; + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowvsdeepcopy/UserWithShallowClone.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowvsdeepcopy/UserWithShallowClone.java new file mode 100644 index 0000000000..19c9b55412 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowvsdeepcopy/UserWithShallowClone.java @@ -0,0 +1,48 @@ +package com.baeldung.shallowvsdeepcopy; + +public class UserWithShallowClone implements Cloneable{ + + private String name; + private Address address; + private int age; + + public UserWithShallowClone(String name, Address address, int age) { + this.name = name; + this.address = address; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public UserWithShallowClone clone() { + try { + UserWithShallowClone clone = (UserWithShallowClone) super.clone(); + return clone; + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/shallowvsdeepcopy/UserCloneUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/shallowvsdeepcopy/UserCloneUnitTest.java new file mode 100644 index 0000000000..f5a7d9df93 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/shallowvsdeepcopy/UserCloneUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.shallowvsdeepcopy; +import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; + +public class UserCloneUnitTest { + + @Test + public void givenUser_WhenCopyCreated_thenCopyIsShallow(){ + Address address = new Address("abc", "444-0000", "pqr", "USA"); + UserWithShallowClone user = new UserWithShallowClone("baeldung", address,32); + + UserWithShallowClone userClone = (UserWithShallowClone)user.clone(); + assertThat(userClone.getAddress()).isEqualTo(user.getAddress()); + } + + @Test + public void givenUserWithDeepCloneMethod_WhenCopyCreated_thenCopyIsDeep(){ + Address address = new Address("abc", "444-0000", "pqr", "USA"); + UserWithDeepClone user = new UserWithDeepClone("baeldung", address,32); + UserWithDeepClone userClone = (UserWithDeepClone)user.clone(); + assertThat(userClone.getAddress()).isNotEqualTo(user.getAddress()); + } +} \ No newline at end of file