3 ms·
This was popular in Java (see libraries with "Foo" interface and "FooImpl" class). Sometimes it really is useful, but it was over-used in Java. A lot of times
by armchairhacker 5y ago
This was popular in Java (see libraries with "Foo" interface and "FooImpl" class).
Sometimes it really is useful, but it was over-used in Java. A lot of times you don't even need a mock.
In fact a lot of times even a singleton is good enough. YAGNI. I don't think the example was good: in the "potential situation" where you actually need a second `UserDefaults`, you could've just find and replace. Maybe for a massive project, but I've never worked on something too big for doing things like to avoid Shift-Ctrl-F.
- marcellus23 5y agoThe same problem exists in Swift. I inherited an iOS project a couple years ago where the original dev over-used protocols like crazy. The result is spaghetti code that's impossible to understand. Luckily we're mostly replacing it with simple encapsulation now.
- liuliu 5y agoRule of thumb is always: if you need it, do it, otherwise, don't. Real world cases could be: 1. If you write a unit tests and really need to have a double of that particular component, then do it. Otherwise, don't. 2. You need to decouple the dependency (maybe these are circular), then do it. Otherwise, just take on it as a dependency.
- ratStallion 5y ago#2 is interesting...can you give an example solving circular dependencies? Even if you had an interface, wouldn't you still have the implementations still causing a circular dependency?
- amw-zero 5y agoYou can turn A->B, B->A into A->I,B->A + A.I=B and that does avoid a circular dependency. A->B means a depends on B, A->I means A depends on an interface, and A.I=B means that B is set as the implementation of the I interface that A depends on. I’m on mobile so that was the best I could come up with. Try it out using Swift though, and that can be translated pretty directly.