MemberwiseClone vs新对象

核甜

我有:

internal sealed class A : ICloneable
{
    public Collection<B> CollectionB { get; set; }

    public int Int1 { get; set; }
    public int Int2 { get; set; }
    public int Int3 { get; set; }
    public int Int4 { get; set; }
    //. . .
    //some different value types, lets think all of type int for simplyfy

    public object Clone()
    {
        //Need to implement Clone
    }

}


internal sealed class B : ICloneable
{
    public Collection<Bitmap> SomeBitmaps { get; set; }

    public int Int1 { get; set; }
    public int Int2 { get; set; }
    //. . .
    //etc.

    public object Clone()
    {
        //same as class A
    }
}

我应该如何在此对象中实现Clone()?在不同的任务中,我需要将深度对象作为DTO进行深层克隆。

我是否应该像(A)那样:

public object Clone()
{
     var clone = this.MemberwiseClone();
     var cloneCollectionB = new Collection<B>();
     foreach(var element in CollectionB)
     {
          cloneCollectionB.Add(((B)element).Clone());
     } 
     ((A)clone).CollectionB = cloneCollectionB;
     return clone; 
}

而对于B同样的方式,还是有不同的好的模式/解决方案?我应该使用new object()而不是MemberwiseClone()手动设置字段吗?谢谢。

德米特里·拜琴科(Dmitry Bychenko)

您可以这样做:

internal sealed class A: ICloneable {
  // Let it be List, OK?
  // It looks, that you don't need "set" here 
  public List<B> CollectionB { get; }

  public int Int1 { get; set; }
  public int Int2 { get; set; }
  public int Int3 { get; set; }
  public int Int4 { get; set; }

  // It's more convenient to return A, not Object
  // That's why explicit interface implementation    
  Object ICloneable.Clone() {
    return this.Clone();
  }

  public A Clone() {
    A result = new A();

    result.Int1 = Int1; 
    result.Int2 = Int2; 
    result.Int3 = Int3; 
    result.Int4 = Int4; 

    // Deep copy: you have to clone each B instance:
    foreach(B item in CollectionB)
      result.CollectionB.Add(item.Clone()); // <- thanks for explicit inteface impl. it ret B

    return result;
  }
}

internal sealed class B: ICloneable {
    // Let it be List
    // It looks, that you don't need "set" here 
    public List<Bitmap> SomeBitmaps { get; }

    public int Int1 { get; set; }
    public int Int2 { get; set; }
    //. . .
    //etc.

    // It's more convenient to return B, not Object
    // That's why explicit interface implementation    
    Object ICloneable.Clone() {
      return this.Clone();
    }

    // It's more convenient to return B, not Object    
    public B Clone() {
      B result = new B();

      result.Int1 = Int1; 
      result.Int2 = Int2; 
      // ...
      //etc.

      // Deep copy: you have to create new bitmaps here
      foreach(Bitmap source in SomeBitmaps) 
        result.SomeBitmaps.Add(new Bitmap(source));

      return result; 
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章