0

Recently I started working on creating a client library. One problem which I am facing now is that I have to move the below interface Attributes to client library but the concrete classes (business logic) should remain in the original library. The problem here is that the method signatures of the below interface are Concrete classes.

   public interface Attributes {

      public Integer getAttribute(IntegerAttr attr);

      public Boolean getAttribute(BooleanAttr attr);

      public String getAttribute(StringAttr attr);

      // some more code.
  }

Each of the above parameter classes implement one common interface called Attr.

class IntegerAttr implements Attr { .. }
class BooleanAttr implements Attr { .. }
...

Now if I try to make the parameters to type Attr in interface Attributes then we will get Override exception because of same method signature.

But I have to move this interface to client library but not the concrete classes.

Any suggestions on this please.

3

Well, it would be easier to answer if you had told us what you are really trying to accomplish (for example, why you are going to move the interface), but anywhere.

A straightforward approach works as follows:

  1. Create interfaces IIntegerAttr, IBooleanAttr and IStringAttr, all derived from Attr.

  2. Make IntegerAttr derive from IIntegerAttr (and the other two analogously).

  3. Change the signature of the getAttribute methods to use the new interfaces.

Now you can move Attributes as well as the new interfaces to the client library, without having to move the concrete classes IntegerAttr, ....

  • Hmm. thought the same. Seems like there is only one way to solve this. – Amarnath Aug 26 '17 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.