0

How can I define and implement an interface where one Module is using it for the implementation (lets say the View layer) and another Module is calling it to get the result from the defined implementation (say the Backend Layer)?

I want to keep seperation of concerns so that the Backend layer does not care about how it is implemented, yet the View Layer must always have an implementation even if there are different implementations of this module.

Say the interface is the following:

interface AreaProcessing{
/**
 *
 * @param point Pair<Double, Double> the point's <Longitude, Latitude>
 * @param areaPointsList ArrayList<Pair<Double, Double>> as of <Longitude, Latitude>
 * @return Boolean True, if the given point is inside the area, False otherwise.
 */
fun isPointInsideTheArea(
    point: Pair<Double, Double>,
    areaPointsList: ArrayList<Pair<Double, Double>>
): Boolean

More specifically, I have a module that is based on the specific implementation of the map engine say GoogleMapModule. I want this module to implement the specific interface and then in the backend to call it without having any knowledge on the map module or the class that is being implemented within. So if I then decide to use another module, say BingMapModule all I have to do is implement the interface ONLY in the new module without affecting the backend. Optionally, if there is no implementation for the specific interface then an exception could be thrown.

I am not sure if this doable with interfaces or with abstract class. That is why I am asking here.

I have read the following post here : If I call an interface method, will it get the method body from implementation class and execute?

Thanks in advance.

RandomType
  • 47
  • 1
  • 8
  • I really don't understand what you're trying to do. The whole point of interfaces is so that you can have different implementations but clients can use them without knowing which one. Perhaps you could show more code to explain what you're trying to do? – ndc85430 Dec 27 '22 at 12:32
  • @ndc85430 I have updated the question to be more precise. Thank you. – RandomType Dec 27 '22 at 12:39
  • From your description, it sounds like you have the right idea. Have you tried it? What difficulty are you having? – ndc85430 Dec 27 '22 at 12:48
  • @ndc85430 Actually i have no idea. I cannot create an instance of the interface as it is. – RandomType Dec 27 '22 at 12:53
  • I think your problem is not the interface or its implementation. You are missing the concept of dependency injection. When using an interface as input of a class or function, you must pass the class that implements it. In other word interface is what you need to be done and the implementation is how you do it. So when you change the implementation, nothing changes in your function or class, because you are still doing the same work no matter how it is done . This is the magic of dependency injection. – A. Hajian Dec 27 '22 at 13:40
  • @A.Hajian Yes! Thats what I am trying to do. I already use DI but I just dont know how to do it for this specific problem. I understand that I must pass the implementation somehow from the `View Layer` to `Backend Layer`. So I can add a constructor parameter in the `Backend Layer` of type `AreaProcessing` and pass it with DI from the `View Layer`? – RandomType Dec 27 '22 at 13:48
  • @A.Hajian Or maybe, create a class that inherits the `AreaProcessing` interface, say `AreaProcessingImpl` in the `View Layer` inside the `GoogleMapModule` and pass this with DI in my `Backend Layer` module. Right? – RandomType Dec 27 '22 at 13:59
  • Yes.the best way is in the back get an interface as a parameter, implement this interface in the view based on your buisness and pss it to the back. On the other hand, ( I don't know your app architecture but) I think processing is something you should consider to implement in the background not the view. – A. Hajian Dec 27 '22 at 14:59
  • @A.Hajian Yes it will be done in the background according to MVVM and clean architecture. – RandomType Dec 27 '22 at 15:26

0 Answers0