mercredi 1 mars 2023

Can we use interfaces when the implementations cannot be accessed via interface

I am little new to programming design techniques. I have two classes like FooHandler and BarHandler.

class FooHandler{
  ...
  fun method1(someArgs){ getMap() }
  ...
  fun method2(someArgs){} 
  fun getMap() {}
}
class BarHandler{
      fun method1(someArgs){ getMap() }
      ...
      fun method2(someArgs){} 
      fun getMap() {}
    }

These two classes have completely different methods with different args. Still these methods use methods like getMap() which has the same implemenation in both Foohandler and BarHandler

In this scenario can I have an interface of type Handler. And make FooHandler and BarHandler implement it and push the method of getMap to the Handler interface with default implementation.

some thing like

interface Handler{
  fun getMap(){}
}

just for code reusability. I will not access the implementations via handler. Does this make sense or we must avoid interface in this scenario?

Aucun commentaire:

Enregistrer un commentaire