-2

I try to program to an interface whenever possible, but when I have a class that fulfills a very specific and unique purpose, one that can't be abstracted. Is it correct for me to say that I've reached the limits of "abstraction" and that I need to just pass in the concrete class?

public void DoSomethingWith (HighlySpecificClass specificClass, ISomeInterface abstractedClass)

So if the "program to an interface" is more a guideline than a hard rule, how can I make it testable? Should I create a "header" interface for it anyway?

profile
  • 25
  • 2
  • I think you can try to implement some base class or interface for your HighlySpecificClass. But if your HighlySpecificClass does not share any function or property with other classes / interfaces by design, I think it should be ok to just use it for the interface definition. – Zeal Lin Jun 29 '15 at 09:22
  • @gnat not exactly, I'm trying to determine the specific case where the design of "what the code is doing" is necessarily highly specific. Would I still make it a "header" interface? – profile Jun 29 '15 at 14:50

1 Answers1

0

'Program to an interface' is a general, language-independent statement. With languages like C# and Java that have an explicit language feature called interface, this can lead to confusion.

The thing to remember is that both languages have at least two ways of defining interfaces;

  • explicitly, with a keyword
  • implcitly, by taking a class and making only the relevant bits accessible (with public etc.).

Knowing when to use which way of defining an interface is in general a judgement call.

Sometimes a specific technology or tool will make the decision for you. For example, if you were using a mocking tool that only supports mocking interfaces, and a particular test needs a thing to be mocked, then you are going to have to use an explicit interface.

But ideally you would use a less limited tool that supported both explicit and implicit interfaces.

soru
  • 3,625
  • 23
  • 15