1

If I have the following generic interface:

 public interface IValidator<T> { Boolean IsValid(T entity_); } 

Can I use it in the following manner? Is this against Object Oriented Programming guidelines?

 public PathValidator : IValidator<String> { } 
2
  • Perfectly fine. Look at interfaces like IComparable. Commented Dec 13, 2010 at 15:16
  • adam, this is kinda similar to a suggestion/proposed interface i made to you the other day. i think you're fine adopting this approach 100% - lol stackoverflow.com/questions/4399447/… Commented Dec 13, 2010 at 15:59

4 Answers 4

6

Is that use of Interfaces against any sort of best practice?

No, this is fine and common (assuming that your interface is not empty and has a method signature like bool IsValid(T entity)).

What makes you think it should be? If you let us know, we can elaborate.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah I have something basically along the lines of bool isValid(T entity). I assumed it might be going against what most people would do as I'm specifying the type on the definition of the class. Which I thought might make you think the generic interface was useless.
@Adam Witko: Well, consider the alternative: interface IStringValidator { bool IsValid(string s); }, interface IFooValidator { bool IsValid(Foo f); }, etc. Is that better?
I'd have to say probably not!
@Adam Witko: Then it's clear now why the generic interface is a Good practice?
0

Of cause not, just take a look at the generic collection interfaces like IList.

Comments

0

If this is actually the body of the interface, it's generally not a good idea to have an interface with no contract (that is, it doesn't require you to implement any methods). Usually this is suggestive of a problem with your design.

Comments

0

An empty interface (Marker interface) is usually bad practice because you should use an Attribute instead.

But if it's only empty because you omitted it to simplify the example then it looks perfectly fine to me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.