Java Inheritance and Extension

Imagine that you are working on a mature product and you are requesting a new search function, which is required for 50% of your product. Now suppose you have an established interface inheritance relationship with SomeDao that you don't want to break ...

public interface MoneyDao extends SomeDao<MoneyEntity> { //Operation common in much of the application List<MoneyEntity> findByCriteria(MoneyCriteria criteria); } 

... is there a way to expose the findByCriteria (..) 'method without repeating it in all other places like MoneyDao, where is this needed in a cleaner way?

I do not want to forget that I do not want to enter a new type where it can be used and modified by SomeDao, if at all possible.

Regards, James

+6
source share
2 answers

Can you break findByCriteria into your own interface and extend it in MoneyDao ? Something like that:

 public interface MoneyDao extends SomeDao<MoneyEntity>, MoneyFinder { } public interface MoneyFinder { //Operation common in much of the application List<MoneyEntity> findByCriteria(MoneyCriteria criteria); } 

Now your class (s) that implements MoneyDao does not need to be changed, but you can only get around findByCriteria with MoneyFinder .

+5
source

It all depends on whether you want a class that is searchable, and is Dao, in other words, if your Searchable class should also be Dao. If in this case I would use a general approach to make your Tao accessible.

 interface SearchableDao<Entity, Criteria> extends SomeDao<Entity> { List<Entity> findByCriteria(Criteria criteria); } 

Your class can now be a simple Dao or SearchableDao. SearchableDao is also a simple tao.

 class MoneyDao implements SearchableDao<MoneyEntity, MoneyCriteria> { List<MoneyEntity> findByCriteria(MoneyCriteria criteria) {...} } 
+1
source

Source: https://habr.com/ru/post/887725/


All Articles