samedi 20 août 2016

Prototype design pattern in c#

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?

Aucun commentaire:

Enregistrer un commentaire