0

I'm looking for assistance on how to implement this Repository. Here's what I have so far:

public interface IEntity {
    int getId(); //would rather not depend on int. fix later.
}

public interface IRepository<T extends IEntity> {

    Collection<T> findAll();
    T find(T t);
    T findById(int id); //would rather not depend on int. fix later.
    void add(T t);
    void remove(T t);
}

public interface ISurveyRepository extends IRepository<Survey> {

}

The problem I'm running into is that I need for T in the IRepository signature to extend IEntity, but I don't need IRepository in the ISurveyRepository signature to have a bounded type parameter. I would like for the signature to just be

public interface ISurveyRepository extends IRepository { }

so that I could create a concrete class that just implements ISurveyRepository

public class MySurveyRepository extends ISurveyRepository { }

How can I go about doing that?

5
  • 2
    What exactly is the reason that you don't want to extend IRepository with the type parameter? Note that you don't implement an interface by extends, but by implements. Commented Nov 18, 2011 at 19:21
  • 1
    Why does your IRepository extend IEntity? Commented Nov 18, 2011 at 19:22
  • IMO Survey should implement IEntity. Commented Nov 18, 2011 at 19:25
  • @BalusC My initial thinking was that if I made IRepository without the type parameter then it would be more generic (I wouldn't have to put a class in the placeholder). After you asked the question I thought about it and it seems to make sense to leave the typed parameter there because then I can just my Survey class in the placeholder. Any thoughts? Commented Nov 18, 2011 at 19:46
  • @TomaszNurkiewicz That was a typo from an older file that I copied from. I've removed it. Commented Nov 18, 2011 at 19:47

2 Answers 2

1

You can do better than this.

Fix that int problem now. Use a generic DAO interface, like this:

public interface Repository<T, K extends Serializable> {
    List<T> find();
    T find(K id);
    K save(T value);
    void update(T value);
    void delete(T value);    
}

Lose that Hungarian notation in disguise: no "I" for "interface".

You can write a generic implementation, too.

Sign up to request clarification or add additional context in comments.

8 Comments

K save(T value) is unlikely to work. You can not execute new K.
@duffymo why did you use List<T> instead of Collection<T>?
K is the key that you return. What do you mean "can't execute new K"?
Why not? What's wrong with List<T>? I find Collection<T> to be too restrictive. Both are interfaces. That was the decision I made when I created it.
As far as recommendations for improving your code, this contains a lot of good information - my answer was restricted to the exact specified domain.
|
1

If you want to create a class like:

public class MySurveyRepository extends ISurveyRepository {}

Then your existing interface (with the use of generics) will do just fine. Your implementing class will 'inherit' the definition by design, and will be (effecticely) completely unaware that it's descended from a previously-generic interface.

If you're using one of the modern editors, like Eclipse, to write your code, when you ask it to fill in the missing inherited methods it won't give you T - it'll give you Survey.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.