Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract properties or base constructor parameters? [closed]

Which method should I prefer and why? Is there any real difference?

Abstract property:

abstract class Table
{
    public abstract string Title { get; }
}

class InfoTable : Table
{
    public override string Title
    {
        get { return "Info"; }
    }
}

or base class constructor parameter:

abstract class Table
{
    public string Title { get; private set; }

    public Table(string title)
    {
        Title = title;
    }
}

class InfoTable : Table
{
    public InfoTable() : base("Info") { }
}
like image 766
astef Avatar asked Nov 12 '22 04:11

astef


1 Answers

As they stand your class are just data class which is not a good OOP practice. Anyway I prefer the first type because if in that case in a subclass you can have a non harcoded title as in this example

class InfoTable : Table
{
    private string id;
    private string name;
    public override string Title
    {
        get { return name+id; }
    }
}

but still it may depend which of the two is better

Okay let's say that the class is not something as static as this example then

private DynamicTitleProvider provider;
public override string Title
    {
        get { return provider.GetTitle(); }
    }
like image 156
Fabio Marcolini Avatar answered Nov 14 '22 23:11

Fabio Marcolini