DEV Community

Cover image for OOP Clone Objects
Hasan Elsherbiny
Hasan Elsherbiny

Posted on

OOP Clone Objects

when you Jump into the world of OOP in which parts of your application should act as objects, following this concept will get us to need to copy data from object to another object for example creating a clone of the same object right?
in this case we have two types of copying

Value Copy Vs Reference Copy

as you know every object is actually a memory allocation for your data.

  • Reference Copy the copy of my object will have the same address of the memory so any change to the copy will be applied to the original object.
  • Value Copy we only need the data to be duplicated into a brand new object (with new memory address) so any change to the copy will not affect the original one.

Default Behavior

by default if your object is a class then any copy to the object is Reference Copy.
and if the object is struct then any copy to the object is Value Copy.

Special Scenarios

commonly we use classes more than structs but also we may have some particular cases in which we need to value copy objects of classes.
so lets assume that we have class Person

 public class Person
 {
     public string Name { get; set}
     public string Age { get; set}
 }
Enter fullscreen mode Exit fullscreen mode

1. data allocation (copy Constructor)

in this method we simply add constructor in which takes the object as input and copy properties values into the new property

 public Person(Person person)
    {
        Name = person.Name;
        Age = person.Age;
    }

Enter fullscreen mode Exit fullscreen mode

this sounds very straight forward but it has some drawbacks first it's quite difficult if your class has many properties and any change to these properties require maintenance, but it's also giving you control over what to copy and what not to.

2. implementing ICloneable interface

sometimes your language can give you out of the box solutions for cloning objects in C# for Example Your class can implement ICloneable interface to indicate that class is cloneable.

  public object Clone()
    {
        var person = (Person)MemberwiseClone();
        return person;
    }
Enter fullscreen mode Exit fullscreen mode

in theses two solutions you had access to the class of object you want to clone, but some cases you may not have access to this class for example if this object if from library.

3. Serialization

using the concept of serialization you can transform your object into another form (string or binary) then deserialize back into an empty object

     var serialized = JsonConvert.SerializeObject(source);
     var newObject=JsonConvert.DeserializeObject<T>(serialized);
Enter fullscreen mode Exit fullscreen mode

this is great idea specially when you don't have access to the code as mentioned before, but in some specific cases this solution would fail, for example if the object is not serializable.

4. Self AutoMap

if your code already using automapper solution you can make a good use of it to self map your object to a new object

var newObject=ObjectMapper.Map<Person>(source);
Enter fullscreen mode Exit fullscreen mode

this littlie trick would work with almost no drawbacks.

Top comments (0)