Thursday, 3 October 2013

Generic class with an interface constraint vs class implementing interface

Generic class with an interface constraint vs class implementing interface

Recently I was implementing a Trie data structure and decided the Nodes
could store different types of data or have its implementation varied so
then I went for Node<T>. Then as I got into the algorithm for constructing
the Trie I realised it required more intimate knowledge of the Node so I
constrained the generic class to use an INode interface. This allows for
more flexibility but felt wrong in the context of a generic class.
Generic classes have a different use case to classes which implement an
interface. For example, List<T> - the algorithm can work without being
dependent on a related set of abstractions. A class which implements an
interface may require polymorphism/DI but the interfaces will be more
specialized.
Under what circumstances do others apply a generic class T where T may
implement a more specialized interface?
I thought that a generic class is used when T does not really need to
expose operations/data though I can see a generic class may be used where
T implements IDisposable or some other more general interface.
Any help in clarifying these points?

No comments:

Post a Comment