Concrete types or interfaces for return types?

Today I came to the fundamental paradox of the style of object programming, specific types or interfaces.

What are the better choices for the type of the return method: a specific type or interface?

In most cases, I prefer to use specific types as the return type for methods. because I believe that a particular type is more flexible for future use and provides more functionality.

The dark side of this: Grip. Angelic: a particular type contains the interface from which you intend to return initially, and additional functionality.

What is the rule of thumb?

Is there any programming principle for this?




BONUS . Is this an example of what I mean ReadOnlyCollection or IEnumerable for publishing member collections?

+6
methods oop design-principles
Apr 21 '10 at 0:02
source share
3 answers

My rules of thumb are:

1) First, I have a method that returns an interface type, because it is always easy to change to a specific type later, if necessary. It is more difficult to return to the other side.

2) Even if a method is returned that returns a specific type, I would encode callers to use the interface type when possible:
InterfaceType i = xyz.methodThatReturnsConcreteType(); .

3) Whether I am the owner of the calling code also matters (internal and public APIs):

  • If I have code that calls this method (i.e. the internal API), then I want to return a specific type.
  • If I do not control the code that calls this method (for example, the public API), the sooner I will return the type of interface. Returning a specific type is a commitment and, generally speaking, the less I promise, the easier.

Other considerations:

  • Testing can be simpler with interfaces, as I can use a mock object that implements the interface.
  • I have an external chance that I want to return the proxy server (now I really get excuses)

In this way,

  • I usually return an interface type because I feel that the benefits of loose communication outweigh the convenience of full access to a particular type.
  • However, I am not opposed to switching to a specific type of return in each particular case, when the convenience outweighs the benefits of a weakened connection.
+4
Apr. 21 '10 at 0:59
source share

The thumb rule, in reverse types, should be as specific as possible, in parameter types should be as undefined as possible. Also prefer interfaces, as you can exchange your implementation later, if necessary, without changing the clients of your API.

+7
Apr 21 '10 at 0:08
source share

Interest Ask. I believe you should ask yourself how you can use the returned data. Using the old car analogy, if you have

 public AccelerationResponse PressAccelerator(float force) {} 

Most likely you want to return an interface, not a class. You may interpret this answer differently depending on certain conditions.

If you are guaranteed that your return can only be used in accordance with the expected specific implementation, then using this class makes sense. I am not sure of the generally accepted principle, but my rule of thumb is that the return type can be reused in different implementations, the interface makes more sense.

+1
Apr 21 '10 at 0:08
source share



All Articles