Is it advisable to have an interface as the return type?

Posted by wb on Stack Overflow See other posts from Stack Overflow or by wb
Published on 2010-06-14T01:59:34Z Indexed on 2010/06/14 2:02 UTC
Read the original article Hit count: 170

I have a set of classes with the same functions but with different logic. However, each class function can return a number of objects. It is safe to set the return type as the interface?

Each class (all using the same interface) is doing this with different business logic.

protected IMessage validateReturnType; <-- This is in an abstract class

public bool IsValid() <-- This is in an abstract class
{
    return (validateReturnType.GetType() == typeof(Success));
}

public IMessage Validate()
{
    if (name.Length < 5)
    {
        validateReturnType = new Error("Name must be 5 characters or greater.");
    }
    else
    {
        validateReturnType = new Success("Name is valid.");
    }

    return validateReturnType;
}

Are there any pitfalls with unit testing the return type of an function? Also, is it considered bad design to have functions needing to be run in order for them to succeed? In this example, Validate() would have to be run before IsValid() or else IsValid() would always return false.

Thank you.

© Stack Overflow or respective owner

Related posts about c#

Related posts about best-practices