1

can anyone explain why we need to use MemberwiseClone() in prototype pattern?

public abstract class AProtagonistEx
{
    int m_health;
    int m_felony;
    double m_money;
    // This is a reference type now
    AdditionalDetails m_details = new AdditionalDetails();

    public int Health
    {
        get { return m_health; }
        set { m_health = value; }
    }

    public int Felony
    {
        get { return m_felony; }
        set { m_felony = value; }
    }

    public double Money
    {
        get { return m_money; }
        set { m_money = value; }
    }

    public AdditionalDetails Details
    {
        get { return m_details; }
        set { m_details = value; }
    }

    public abstract AProtagonistEx Clone();
}



class CJEx : AProtagonistEx
{
    public override AProtagonistEx Clone()
    {
        **return this.MemberwiseClone() as AProtagonistEx;**
    }
}

By default all the properties and methods of the parent class can be access in the child class. then what is the need of this pattern?

Tom Cruise
  • 1,395
  • 11
  • 30
  • 58
  • Because you need to create a *copy* of the prototype, not a reference. Otherwise you'd simply have multiple references to the same objects, and any changes would be visible to all of them. – Luaan Aug 20 '16 at 08:51
  • what is the difference between copy and reference? – Tom Cruise Aug 20 '16 at 09:08
  • A reference, as the name suggests just a reference to a memory location (like pointers in C/C++, this concept is not limited to .NET or C#). However, a copy, as the name suggests a duplicate of the whole memory region (pointed by the reference) copied to another memory location. The contents of the memory location pointed by the reference can be changed by the time which has of course a catastrophic results. Have a look at this http://www.albahari.com/valuevsreftypes.aspx and also for more theoritical reading https://en.wikipedia.org/wiki/Reference_%28computer_science%29 – Deniz Aug 20 '16 at 09:15
  • even in MemberwiseClone() only top level objects will be cloned and values will not be copied. – Tom Cruise Aug 20 '16 at 09:20
  • @TomCruise Again there are two kinds of copying/cloning operation. Deep vs shallow. Have a look at https://www.cs.utexas.edu/~scottm/cs307/handouts/deepCopying.htm and http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy. – Deniz Aug 20 '16 at 09:25
  • 1
    Shallow copy is not an exact copy of the source object, because in shallow copies the contents of the memory locations would not get copied. Just the references are copied. However, in deep copy in addition to the references, all the memory locations pointed by these references also copied into the new object. If it seems complicated read the links or search for more on Google. You can find lots of information. – Deniz Aug 20 '16 at 09:25
  • @TomCruise Be aware that JavaScript / ECMAScript and its prototype based class definitions/inheritance are not the same as the prototype design pattern (which .NET / SystemIClonable / Object.MemberwiseClone support) – Thomas Aug 20 '16 at 10:38

2 Answers2

4

Prototype Design Pattern is about instances, not about classes. Instances of CJEx class do indeed inherit all properties and methods of their base class through inheritance. However, the prototype pattern is concerned with the values of the properties, not simply with having the properties on the object.

In fact, this is the difference between the prototype pattern and the abstract factory pattern: the prototype pattern pre-populates your properties in the way they are set in the prototype object, while abstract factory gives you an object with properties that set to default values or the values that you provided in the call.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • The same can be achieved by copying the objects.Then what is the need of clone here? – Tom Cruise Aug 20 '16 at 09:24
  • @TomCruise "The same can be achieved by copying the objects" Not really: in order to make a copy you need to know the exact type of the object. Clone lets you copy a subclass without knowing its actual class, or even without having access to the constructor. In other words, cloning is copying directed by the object itself. – Sergey Kalinichenko Aug 20 '16 at 09:27
  • var obj1=obj; what is the difference between this and MemberwiseClone()? I dont need to know the object type here. – Tom Cruise Aug 20 '16 at 09:35
  • 2
    @TomCruise Do you understand the difference between a copy and a reference? – Sergey Kalinichenko Aug 20 '16 at 09:38
  • 2
    @TomCruise please look at my comments and try to understand the differences between copy/reference and shallow/deep copies. These are basic concepts for your question. – Deniz Aug 20 '16 at 09:40
0

MemberwiseClone Is I’m used to make the shallow copy of an object.

Reference type members(example string) of both copied and the original object will have to same reference. So if user modifies one, it will change the other as well.

To make the deep copy user need to add a custom copy method. So that both members will have different references.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197