mercredi 22 septembre 2021

java callable with multiple methods

I`m trying to implement a java callable with multiple methods and my situation is like this:

interface FsHandler {

  boolean existDirectory();
  boolean existFile(File);
  ...
}
interface FileHandler {

  void write(byte[]);
  void read(byte[]);
  ...
}

All these methods are implemented by different storage backends like local file system, aws s3, etc

While search for a way to parallelise this code, I tried to use callable but if I understood correctly, I would need to to break my interface in many small abstract classes that implements callable - one for each method and then use callable, not to mention that I would still need to find a way to add the call to the blocking queue (which is using an executor)

So, my doubts are:

  • is it possible to wrap callable on already existing/implemented methods?
  • another idea is to create an abstract class for each method that needs to implement callable and then use the strategy pattern to decide which implementation to use

e.g.:

interface WriteStrategy implements Callable<Void> {

  Void call();
  void write(byte[]);
  static WriteStrategy localFSStrategy(){...}
  static WriteStrategy S3Strategy() {...}

class FileHandler {
  private WriteStrategy strategy;

  public FileHandler(WriteStrategy strategy) {...}
  public void write(byte[]) { strategy.call(); }
}

I`m still not sure how to approach it :'(

Aucun commentaire:

Enregistrer un commentaire