Hello, on my ongoing learning of Interfaces I'm now reading about the ICloneable interface, which its quite easy to understand (the basics anyway!)
well in the book I'm reading the author gave an example of how to clone a class that implements the ICloneable interface. cloning basic class is quite simple, just return the MemberwiseClone and you get (not true) deep copy:
well that's only work if you don't have any references to other classes inside the class you're copy, if you do those references will be shallow copy (only the references to memory will get copied)Code:public object Clone() { return this.MemberwiseClone(); }
what the author suggested to do is to first get shallow copy:
and then start to copy the reference class and fill the gaps:Code:Point newPoint = (Point)this.MemberwiseClone();
it looked to me like hard work if the reference class has a lot of properties, what i did instead is to implement the ICloneable interface in the reference class as well, and return its MemberwiseClone too, like so:Code:PointDescription currentDesc = new PointDescription(); currentDesc.PetName = this.desc.PetName; newPoint.desc = currentDesc; return newPoint;
which work as well with a lot less work.Code:public object Clone() { Point CPoint = (Point)this.MemberwiseClone(); PointDescription desc = new PointDescription(); desc = (PointDescription)this.test.Clone(); CPoint.test = test; return CPoint; }
so my question is basically is, is it bad idea to use the ICloneable interface like i did? is it consider bad practice ?
is using the ICloneable reduce my app performance ?
Thanks!




If you Like it 
Reply With Quote