Is it okay to inherit from a class just for clarity?

Suppose I have a template / generic class that simply stores key / value pairs:

public class GenericDatabase<Key, T>
{
 public Dictionary<Key, T> Data { get; set; }               

 public GenericDatabase()
 {
   Data = new Dictionary<Key, T>();
 }
 ...
}

      

Is it acceptable to derive a class from it without introducing any new methods or member variables just for clarity? For example, let's say I want to have a database of symbols:

public class CharacterDatabase : GenericDatabase<string, CharacterStat>
{
   // no new methods or member variables
}

      

+3


source to share


4 answers


IMO Implementing an inheritance level for clarity will confuse other developers. I think the general declaration is clear enough.



GenericDatabase<string, CharacterStat>

      

+3


source


It's a different language .. but look at how C ++ defines a classstd::string

.

typedef basic_string<char> string;

      

Just wanted to point out that this practice is not just common, but is used by the standard library writers themselves.



I would support such a move if you are going to use a CharacterDatabase

significant number of times. Also, I'm not sure if C # provides any other simpler mechanism than inheritance for this (C ++ for example typedef

), but that would be desirable.

The using directive can be used as a typedef , but only affects the file in which it is declared.

using CharacterDatabase = GenericDatabase<string, CharacterStat>

      

+1


source


In OOP, nothing would stop you from doing this. However, I've seen in many places that they use Marker Interfaces rather than a base class to indicate something. Marker interfaces usually do not have a public member and will only be used to mark a class as an example of something.

You can see these links:

Wikipedia => marker interface template

What is marker interface

+1


source


For me it depends on the specific types.

If I had

GenericDatabase<string, CharacterStat>

      

I would leave it as it is - that's understandable. But if I had

GenericDatabase<string, ArrayList<Pair<CharacterStat,Integer> > >

      

then I would definitely give it a name and really explanatory;).

+1


source







All Articles