OTOH in certain specific cases I see the value of the interface even for just one class when I want to clearly separate the contract (which I'm bound to fulfill now and in the future) and the implementation specific comments.
Let's say that in my little pet programming language I have just one List type. I want to clearly separate what's the contract (indexed ordered sequence) as opposed to "incidental" implementation details (constant cost random index access).
EveryClass : IEveryClassSure, I'm adding this shitty console based logger, but since it's wrapped in ILogger I'll later come and write a FileBasedLogger, a DBLogger and a KafkaProducerLogger for when we go webscale.
Except, that never happens.
//Names changed slightly to protect the wicked
public interface IProvideBoolean {
Boolean True { get; }
Boolean False { get; }
}
public class BooleanProvider : IProvideBoolean {
public Boolean True => true;
public Boolean False => false;
}
So.... SOLID....
If there's ever a third value for Boolean, I Will Be Ready.But you want to, that's the point. You're making a drastic change to your code base, surely you want to inspect every place where that type is used.
No, it's the opposite. IClass is the norm, for legacy reasons going all the way back to the early COM days (it all started with IUnknown[1]).
[1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms6...
You can also do "pImpl", which is similar, if not worse, in terms of boilerplate, but doesn't allow DI.
Just don't use these techniques unless you can prove they're needed (e.g private members drag new problematic dependencies, #include <some_volatile_header_file.h>).
It is to me. So far it seems that everyone takes this for granted. I disagree: I have yet to see a problem created by the "each class has an interface" rule, and I have seen countless problems created by classes without one.
So: what problems are caused by interfaces with only one implementation?
> Please note that I’m not saying that interfaces are always bad. When they add value, they are useful. I’m only saying that interfaces that mirror one and only one class implementation is waste.
That's not really controversial or interesting.
"Don't write interfaces that have only one implementation" is something I've heard many times. The only well-accepted exception to that rule is interfaces that are expected to be implemented by, say, users of a library you are writing.