As I've learned that it is not advised to implement ICloneable
(due to the fact that it does not differentiate between Deep Copy or Shallow Copy), I'm trying to determine whether I should implement it as an abstract or an interface.
I feel that my implementation would stay largely the same, e.g. a binary deep copy and a MemberwiseClone
shallow copy, so to that end I felt that an abstract method would be ideal. However, my understanding is also that C# does not do Multiple Inheritance, thus if I ever need to use another abstract class, then I no longer can.
In that case, I feel that implementing a custom ICloneable
(e.g. ICustomCloneable
) would be the better option, but if the implementation is effectively the same across many classes, I feel like I'm not adequately taking advantage of code reuse.
That being said, is it valid to use an interface to keep the abstract inheritance clear for more important things in my cloneable classes? Or is there another way to do this?
Alternatively, is it valid (read: not smelly) for an abstract to implement another abstract? This would be my guess as to getting around the single-inheritance that prevents me from implementing the CloneCapable class as well as another abstract, but it sounds like it might be questionable. e.g.:
public abstract class CloneCapable
{
public object ShallowCopy()
{
// implementation
}
public object DeepCopy()
{
// implementation
}
}
public abstract class ClassA : CloneCapable {}
// abstract-abstract since I can't do ClassB : ClassA, CloneCapable
public abstract class ClassB : ClassA {}