3

Suppose I have a template/generic class that just store 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 instance, say I want to have a character database:

public class CharacterDatabase : GenericDatabase<string, CharacterStat>
{
   // no new methods or member variables
}
Extrakun
  • 19,057
  • 21
  • 82
  • 129

4 Answers4

3

IMO Introducing a level of inheritance for the sake of clarity would confuse other developers. I think Generic declaration is clear enough.

GenericDatabase<string, CharacterStat>
Habib
  • 219,104
  • 29
  • 407
  • 436
  • I would like to understand better; why would it be confusing? – Extrakun Feb 04 '13 at 08:53
  • @Extrakun, Suppose I am going to use your class in my code, then just by typing the class name `CharacterDatabase`, I won't be able to see if it is inherited from a generic class, Since you have the generic class in your code, I suppose you would use it for multiple types. Leaving it as a Generic class will let the developer know that there are other possible types which could interact with your class `GenericDatabase`. – Habib Feb 04 '13 at 09:10
1

Its a different language.. but take a look at how c++ defines its std::string class.

typedef basic_string<char> string;

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

I would support such a step if you are going to use CharacterDatabase a significant number of times. Additionally I am not sure if C# provides any other simpler mechanism than inheritance for this (like C++'s typedef) but that would be prefered.

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

using CharacterDatabase = GenericDatabase<string, CharacterStat>
Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
1

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

You can take a look at these links:

Wikipedia => marker interface pattern

What is a marker interface

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
  • I haven't worked with Java, thus I really don't know. But as the name suggests, `Cloneable` means that you should be able to call `instance.Clone()` method. Thus it shouldn't be actually a marker interface. – Saeed Neamati Feb 04 '13 at 06:35
  • What you suggest is what most developers would (justifiably) think. However, it is a marker interface with no methods. – cdmckay Feb 04 '13 at 06:39
  • Which is much lamented. Introducing a Cloneable interface which doesn't actually have a Clone() method was a huge mistake. (Although, even if it did have a Clone() method, it'd still be messed up because there's no way to differentiate between deep and shallow cloning.) – Matthew Watson Feb 04 '13 at 08:52
1

For me it depends of concrete types used.

If I had

GenericDatabase<string, CharacterStat>

I would leave it as it is - it is clear. But if I had

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

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

Grzegorz
  • 504
  • 1
  • 3
  • 14