Hi everyone, I would like to ask for your opinion on interfacing in Clojure.
Previously, when working on an OOP language such as Java, my first instinct when dealing with “layered” code is by using interface. Each layer is communicating by interface, and which implementation is used usually determined by which concrete class implementing those said interface is injected. So that my code would pretty much like:
class OuterLayer {
private InnerInterface innerLayer;
public OuterLayer(InnerInterface injectedObject) {
this.innerLayer = injectedObject;
}
public int calculateSomething() {
return this.innerLayer.Calculate();
}
}
How does Clojure deals with this kind of needs? I have a logic layer that really doesn’t care which kind of storage that the storage layer use as long as it returns the data that it needs. I’ve managed this far with Clojure (which is not that long) by ignoring the interfacing concept, I just require a namespace that provides functions for storing objects and call its functions. But lately I’ve been thinking that what if I decided to discard those namespaces, would simply requiring different namespace which has exactly the same function names and signatures suffice?
Isn’t that what Protocol does? How would I “inject” and decide which implementation to use? Is Dependency Injection not idiomatic in Clojure?