Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the author define a blank interface in a project?

Tags:

android

kotlin

The following code is from the project https://github.com/skydoves/Pokedex

I can't understand why the author need define a blank interface Repository.

What are the benefit using a blank interface Repository ?

Repository.kt

/** Repository is an interface for configuring base repository classes. */
interface Repository

DetailRepository.kt

class DetailRepository @Inject constructor(
  private val pokedexClient: PokedexClient,
  private val pokemonInfoDao: PokemonInfoDao
) : Repository {
   ...
}

MainRepository.kt

class MainRepository @Inject constructor(
  private val pokedexClient: PokedexClient,
  private val pokemonDao: PokemonDao
) : Repository {
   ...
}
like image 728
HelloCW Avatar asked Jun 29 '21 03:06

HelloCW


People also ask

What is the point of an empty interface?

(Every type implements at least zero methods.) Empty interfaces are used by code that handles values of unknown type. For example, fmt. Print takes any number of arguments of type interface{} .

Can an interface be empty?

A type implements an interface by providing implementations for the members of the interface. An empty interface does not define any members. Therefore, it does not define a contract that can be implemented.

What is a marker interface C#?

Marker interface is a total blank interface that has no body/data-members/implementation. A class implements marker interface when required, it is just to "mark"; means it tells the JVM that the particular class is for the purpose of cloning so allow it to clone.

What is the difference between an interface and a marker interface?

An interface has two uses that work together: First, it describes the members that a class implements. Second, it allows us to cast a class that implements an interface as that interface. Marker interfaces do neither. They allow us to cast an object as a type with no members.

Who is the author of interface management?

Morris, P. W. G. (1979). Interface management—an organization theory approach to project management. Project Management Quarterly, 10 (2), 27–37. DR. PETER W. G. MORRIS

Why do I have empty interfaces in my design?

If your design includes empty interfaces that types are expected to implement, you are probably using an interface as a marker or a way to identify a group of types. If this identification will occur at run time, the correct way to accomplish this is to use a custom attribute.

Which of the following is an example of an interface?

Examples of interfaces arising from process technology are: Acceptance of project scope definition/brief (systems/configuration analysis) Acceptance of basic engineering/design Design: manufacturing interface Design: construction interface Plant start-up/product delivery quality check


2 Answers

It is called Marker interface pattern. It is interface without any methods or constants. Usually interfaces like this are created to provide special behaviour for a marked classes. You can find a few interfaces like this in java. For example Cloneable and Serializable.

In case of Pokedex i think the reason is much simpler. It looks like this interface was created just to group all repositories. Repository abstraction is never used in the project. Author is always using specific implementations. Repository interface is redundant but can be useful when we want to find all repositories in the project :)

like image 131
lukjar Avatar answered Oct 21 '22 09:10

lukjar


I believe it's because of generics. If you want to create/use an object with generics, the interface will help you a lot.

Consider the following:

interface Animal
class Dog(name:String): Animal{
   fun bark(){
     println("woff")
   }
}
class Cat(name:String): Animal{
   fun meow(){
     println("meow")
   }
}
class Car(name:String){
   fun horn(){
     println("beep")
   }
}
class Farm{
   private val animals = ArrayList<Animals>()
   
   fun addAnimal(animal: Animal){
      // Even if you don't need a direct function from animal
      // you can still store only animals and not cars!
      animals.add(animal)
   }
}
class Main{
   fun main(){
      var farm = Farm()
      farm.addAnimal(Dog("rex"))       // OK
      farm.addAnimal(Cat("luna"))      // OK
      farm.addAnimal(Car("bentley"))   // ERROR
   }
}

Like this, you can enforce the developer to use the tool as you thought of it, avoiding populating objects with general-purpose items.

A further thought could be if you wanted to add some functionalities and you see at some point, that your class repository actually needs a function for each implementation. If you already have an interface, you won't have to worry to search for all possible implementations (maybe the implementation is in one project extending yours), as the compiler will tell you what is missing.

like image 33
Andrea Avatar answered Oct 21 '22 09:10

Andrea