1

I have 3 entities which are similar, but have different ways of doing certain things.

I use interfaces often and find them very useful, but here I chose a base class design for this approach, because the 3 share plenty of common code.

Only one method is public, and this sits on the Spender base class as the 'gateway':

public void SpendAmount(int budget)

My colleagues say I should create an interface for this base class, ISpender, with just the one method.

My question is - why? I've already achieved:

  • Polymorphism
  • Reduced repetition
  • Encapsulation (only the one method is callable)

I can't see why anyone would benefit from mocking ISpender for testing.

The design is open for extension (a new type of spender can inherit from Spender)

What's the real added benefits of also incorporating an interface into this design specifically?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
FBryant87
  • 4,273
  • 2
  • 44
  • 72
  • 3
    Looks like a case for https://softwareengineering.stackexchange.com. – Uwe Keim Feb 20 '17 at 14:10
  • You can box yourself into a class hierarchy if you you're not careful in how you design your inheritance. What's worse is that it might not happen for a very long time--so long that you can't afford to refactor the code. – Kenneth K. Feb 20 '17 at 14:11
  • 1
    Why not use something like the Strategy pattern and use composition over inheritance. – Steve Feb 20 '17 at 14:11
  • 1
    I hate interfaces for no good reason, that being said, be sure you do not need to support multiple inheritance etc. And yes I agree with Uwe Keim, it is a better general software debate. – Trey Feb 20 '17 at 14:13
  • 1
    If your design goals include factors that both approaches support, why not use both? remember, interfaces are to *define* (or declare) behavior, and often, you need to declare that multiple disparate entities that have little or no relation to one another must behave in a certain way. – Charles Bretana Feb 20 '17 at 14:13
  • If you don't need it there is no reason to clutter your code base with interface noone will ever use. If there is a reason for it like mocking for unit tests ("benefit from mocking ISpender for testing"), then you need it and there is no discussion. If it's internal class, you can probably do without it. If it's public API, you will probably end up better off if you declare it for forward compatibility. – slawekwin Feb 20 '17 at 14:27

0 Answers0