Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Interface Inheritance and Extending

Imagine you are working on a mature product and a new search feature is requested that is required for 50% of your product. Now assuming 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 method 'findByCriteria(..)' without repeating it in all the other places similar to MoneyDao where it's required in a cleaner way?

Bare in mind I want to avoid casting in to a new type where its used and modifying SomeDao if at all possible.

Regards, James

like image 453
JARC Avatar asked May 10 '11 14:05

JARC


2 Answers

Can you break the findByCriteria into its own interface and extend it in MoneyDao? Something like this:

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(es) implementing MoneyDao don't need to change, but you can pass around just the findByCriteria using MoneyFinder.

like image 87
Jonathon Faust Avatar answered Sep 19 '22 17:09

Jonathon Faust


Its all depends on if you want a class that is searchable and is a Dao, in other words its if your Searchable class must also be a Dao. If its this case I would use a generic approach to make your Dao Searchable.

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

Now your class can be a simple Dao or a SearchableDao. SearchableDao is also a simple Dao.

class MoneyDao implements SearchableDao<MoneyEntity, MoneyCriteria> 
{
    List<MoneyEntity> findByCriteria(MoneyCriteria criteria) {...} 
}
like image 21
Lynch Avatar answered Sep 18 '22 17:09

Lynch