jeudi 16 septembre 2021

Should I always program to an interface or to supertype? [closed]

Good OOP encourages programming to an interface or supertype. So Basically the design principle is like below

interface Repository{
// some abstract functions 
}


class SomeRepository implements Repository{
// implement methods in repository
}


public static void main(String [] args){
Repository repository = new SomeRepository; 
// call methods in the repository interface which do some stuff
}

So suppose I have multiple repositories which is usually the case in most projects. Each repository has different methods e.g. LoginRepository has login related methods, AuthenticationRepository has authentication-related methods.

I can't program to one supertype like in the example above as the concrete repositories have different methods. How do I handle this use case? Should I create an interface or abstract class for every repository?

But that creates more boilerplate if the supertype is not being used in many places? Can this case be an exception for not following the programming to a supertype principle or there is some design pattern that can solve my issue?

Aucun commentaire:

Enregistrer un commentaire