How to make aninterface not too specific

46 views Asked by At

I am creating a class that is supposed to upload files to many a S3 Bucket with a presignedUrl. For now, I just have to upload files to a S3 Bucket. But in the future, i will have to upload files in others systems.

public interface uploader {

    public void upload(String presignedUrl, InputStream inputStream) throws Exception;
}

My concern is the first parameter String presignedUrl and this : throws Exception I think it is too specific and i dont think my futur implementations will fit this interface.

How can i design my interface to be less specific ?

I simplified like this :

public interface uploader {

    public void upload(InputStream inputStream) throws Exception;
}

But now, i dont know how to get my presigendUrl

2

There are 2 answers

2
dev_fondue On

Your interface is not too specific. It has only single method (aka functional interface) - by definition smallest and most composable abstraction. You will easily create new implementation in future.

But throws Exception is too generic. There are 3 ways to change it:

  1. Consider replace it with RuntimeException instead of just generic checked Exception.
  2. Use custom exception like UploadException extended from RuntimeException.
  3. Or remove exception from interface contract at all.

Couple more tips:

  1. Start interface name with capital letter (like Uploader, not uploader). It is Java convention.
  2. Use wrapper class for upload method arguments insead of String presignedUrl, InputStream inputStream.
0
Reilas On

"... But now, i dont know how to get my presigendUrl"

That would be left to the implementation.

class S3Upload implements uploader {
    String presignedUrl;
    
    @Override
    public void upload(InputStream inputStream) throws Exception {

    }
}

"... How can i design my interface to be less specific ? ..."

interface uploader {

    void upload() throws Exception;
}

Here is the Java tutorial on interfaces.
Interfaces (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance).