Timeline for Should an abstract class implement an interface, as opposed to defining its own abstract methods?
Current License: CC BY-SA 4.0
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 18, 2019 at 12:04 | vote | accept | Eoin | ||
| Apr 18, 2019 at 12:04 | comment | added | Eoin | Cool, thanks for the detailed explanations. They've helped a lot! | |
| Apr 18, 2019 at 11:11 | comment | added | Greg Burghardt | Redundant? Maybe. Harmful? No. You can always copy the method stubs from the Repository<T> interface to the ReportRepository interface. | |
| Apr 18, 2019 at 9:19 | comment | added | Eoin | Thank you. But in your example, both CassandraRepository and ReportRepository implement the Repository<T> interface. Isn't that redundant? Since CassandraReportRepository is extending and implementing both of these classes, shouldn't only one of them need to implement Repository in order for the concrete class to have access to both the save and delete ,methods? | |
| Apr 17, 2019 at 20:05 | comment | added | Greg Burghardt | @Eoin: I updated my answer to address your question. | |
| Apr 17, 2019 at 20:04 | history | edited | Greg Burghardt | CC BY-SA 4.0 | added 485 characters in body |
| Apr 17, 2019 at 19:58 | comment | added | Greg Burghardt | Almost correct. You need to define a more concrete interface for ReportRepository. I can update my answer. | |
| Apr 17, 2019 at 18:20 | comment | added | Eoin | So if we were doing this in Spring, in the class that callsReportService, say a controller class, we'd autowire in something like private ReportRepository reportRepository, and pass that into the constructor? Is that correct? | |
| Apr 17, 2019 at 12:36 | comment | added | Greg Burghardt | @Eoin: Usually a dependency injection container is configured to serve a specific concrete instance for an interface, otherwise the code that creates an instance of the ReportService needs to do this. If you aren't using a DI container, then creating a factory class to return your repositories typed to their interfaces is a good solution. | |
| Apr 17, 2019 at 12:27 | comment | added | Eoin | Thanks, this is really helpful and clarifies a lot of my questions. I'm unsure about one thing though. In your ReportService class, you declare the type of repository as the interface type, for reasons you explained. But how does it know which concrete to use at runtime then? Where is ReportRepository actually used? Is that the object that will passed into the ReportService constructor? | |
| Apr 17, 2019 at 12:04 | history | answered | Greg Burghardt | CC BY-SA 4.0 |