2

I have two implementations of an interface

 EncryptionService
  **ABCEncryptionServiceImpl**
     encrypt(byte[] bytes)
     decrypt(byte[] bytes)
  **XYZEncryptionServiceImpl**
     encrypt(List<String> strings)
     decrypt(List<String> strings)

What I don't like is I have to:

Provide empty implementation in ABCEncryptionServiceImpl for methods:

encrypt(List<String> strings)
decrypt(List<String> strings)

Provide empty implementation in XYZEncryptionServiceImpl for methods:

encrypt(byte[] bytes)
decrypt(byte[] bytes)

Any ideas on how this issue should be handled in a better way i.e using Generics?

1
  • I don't understand. Why do they have to be empty? Do you want some default behavior? Commented Jul 23, 2015 at 23:16

4 Answers 4

1

Instead of having different methods that take specific types such as byte[] and List<String>, you can declare a generic type parameter in EncryptionService.

public interface EncryptionService<T>
{
    public void encrypt(T obj);
    public void decrypt(T obj);
}

The implementation classes will define the type parameter to be a concrete type. This way, neither implementation class will need to provide implementations for encrypting/decrypting a type it doesn't cover.

public class ABCEncryptionServiceImpl implements EncryptionService<byte[]>
{
    @Override
    public void encrypt(byte[] obj) { /* implementation */ }
    @Override
    public void decrypt(byte[] obj) { /* implementation */ }
}

public class XYZEncryptionServiceImpl implements EncryptionService<List<String>>
{
    @Override
    public void encrypt(List<String> obj) { /* implementation */ }
    @Override
    public void decrypt(List<String> obj) { /* implementation */ }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Does T always have to be a mutable class, since you are returning no result ?
@Dici It depends on the OP's actual return type, which hasn't been specified.
yep, which is why it is safer to makethe return type generic !
1

You could add a generic type argument to EncryptionService:

abstract class EncryptionService<T> {
    public abstract void encrypt(T item);
    public abstract void decrypt(T item);
}

You could then specify what data is to be encrypted/decrypted by using it as a type parameter when extending EncryptionService:

class ABCEncryptionServiceImpl extends EncryptionService<byte[]> {
    public void encrypt(byte[] bytes) {

    }

    public void decrypt(byte[] bytes) {

    }
}

class XYZEncryptionServiceImpl extends EncryptionService<List<String>> {
    public void encrypt(List<String> items) {

    }

   public void decrypt(List<String> items) {

   }
}

Comments

0

Isn't it as simple as that ? If I missed something in your question, just tell me.

public interface EncryptionService<INPUT, OUTPUT> {
     OUTPUT encrypt(INPUT input);
     INTPUT decrypt(OUTPUT output);
}

public class ABCEncryptionServiceImpl extends EncryptionService<List<String>, List<String>> {
     ...
}

public class XYZEncryptionServiceImpl extends EncryptionService<byte[], byte[]> {
     ...
}

3 Comments

what if input is a List and output is a Map for ABCEncryptionServiceImpl
@Julie Then just change the second type to Map : ABCEncryptionServiceImpl extends EncryptionService<List<String>, Map<String, String>> my answer is actually the only one hanle to handle this case. All the other answers have chosen a void return type, which won't work for every generic type
@Julie Pleased to help you ! You can accept the answer if it is helpful :D
0

If I get you right, you want to make the parameters for encrypt and decrypt generics.

The naive solution would be

public interface EncryptionService<T> {
    void encrypt(T data);
    void decrypt(T data);
};

Then your implementations would be like

public class ABCEncryptionService implements EncryptionService<byte[]> {
    void encrypt(byte[] data){} 
    void decrypt(byte[] data){}
}

And

public class XYZEncryptionService implements EncryptionService<List<String>> {
    void encrypt(List<String> data){}
    void decrypt(List<String> data){}
}

4 Comments

byte[] is not a primitive type, it is an Array object .You can use it with generics
@dici that's worth a -1? Tough crowd.
Well, maybe I downvote a bit too fast, but I remove it as the answer gets fixed. It also happens to me when I provide a wrong answer !
I work with like 12 different languages. Can get confusing sometimes. My primary is the .net stack. Arrays are handled differently there than java. No problem though. It needed to be corrected for sure, but all it takes is a comment.

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.