jeudi 24 novembre 2016

How to divide logic of returned object's creating between base and derived class in C#?

How to divide logic of object's creating between base and derived class without redundant methods, copying objects, passing by reference and other dirty hacks? This is example. I need write more details. I need write more details. More details.

public abstract class Base
{
    protected int Property1;
    protected int Property2;

    public View GetView()
    {
        View view = new View();
        view.Property1 = Property1.ToString();
        view.Property2 = Property2.ToString();
        return view;
    }
}

public class Derived1 : Base
{
    int Property3;

    public override View GetView()
    {
        View1 view = new View1();
        view.Property3 = Property3.ToString();
        //return view;
        //return base.GetView();
        //return view UNION base.GetView(); ???
    }
}

public class Derived2 : Base
{
    int Property4;

    public override View GetView()
    {
        View2 view = new View2();
        view.Property4 = Property4.ToString();
        //return ???
    }
}

public abstract class View
{
    public string Property1;
    public string Property2;
}

public class View1 : View
{
    public string Property3;
}

public class View2 : View
{
    public string Property4;
}

Thanks.

Aucun commentaire:

Enregistrer un commentaire