0

Scenario:

  1. I have two Libraries which are essentially doing same things but in a different way
  2. Their parameter types, return types and even method names are different from each other.
  3. I need to design a class which at the very beginning decides whether it needs to chose Library A or Library B for its working throughout the code

I am using an abstract class with generics to define the template of the common interface

public abstract class LibraryInterface<A,B,C> {

    public abstract List<B> methodA(A a,B b,C c);

    public abstract List<C> methodB(B b);

    public abstract List<A> methodC();
}

It has 2 implementations, each for Library A and Library B

public class LibraryA extends LibraryInterface<ObjectA,ObjectB,ObjectC> {
    
    @Override
    public List<ObjectB> methodA(ObjectA a,ObjectB b,ObjectC c) {
         // do something
    }

    @Override
    public List<ObjectC> methodB(ObjectB b) {
         // do something
    }

    @Override
    public List<ObjectA> methodC() {
         // do something
    }

}
public class LibraryB extends LibraryInterface<ObjectD,ObjectE,ObjectF> {
    
    @Override
    public List<ObjectE> methodA(ObjectD d,ObjectE e,ObjectF f) {
         // do something
    }

    @Override
    public List<ObjectF> methodB(ObjectE e) {
         // do something
    }

    @Override
    public List<ObjectD> methodC() {
         // do something
    }

}

Problem:

  1. I need to chose a particular implementation at the start of my client code and then use it throughout.
  2. However I cannot couple my client code with specific implementation of a Library.

How do I design my client code to effectively use these libraries.

UPDATE: 1.Plugin system does not solve for my client code issue which needs to heavily work with one of the library implementations

Vishal
  • 21
  • 6
  • Does this answer your question? [Best way to build a Plugin system with Java](https://stackoverflow.com/questions/465099/best-way-to-build-a-plugin-system-with-java) – aled Aug 25 '23 at 15:18

1 Answers1

0

You can hide the logic for choosing between LibraryA and LibraryB in a factory method.

Pino
  • 7,468
  • 6
  • 50
  • 69