我正在尝试解耦第三方库的抽象类。我想公开一个新的抽象类,它将暴露给用户而不是库提供的类 我最初尝试使用适配器,但仍然在适配器类中添加第三方库的导入。
我在下面添加了代码,解释了我的新方法。
// third party lib
abstract class ThirdParty<S> {
public abstract S doAction(S s);
}
// my goal here is to expose a different abstract class which is decoupled from third party lib
// exposed to other modules, rather than exposing the third party lib
abstract class ExposedAbstractClass<S> {
public abstract S doAction(S source);
// get hold of type using guava lib
private final TypeToken<S> typeToken = new TypeToken<S>(getClass()) { };
public Class<S> getSourceClass() { return (Class<S>) typeToken.getClass()
}
// internal class
class Builder<S> extends ThirdPartyLib<S> {
ExposedAbstractClass exposed;
public Builder(ExposedAbstractClass exposed) {
this.exposed = exposed;
}
@Override
public S doAction(S s) {
return (S) exposed.doAction(s);
}
}
//my approach breaks here when i try to invoke builder
class InvokeThirdParty {
public void invoke (ExposedAbstractClass exposed) {
Class type = exposed.getSourceClass();
Builder<type> builder = new Builder(exposed); //doesn't work since Class is runtime type, and T is compile time type
}
}
任何关于这里遵循哪种设计模式的指导都会非常有帮助。
答案 0 :(得分:0)
我同意GuaravJ的答案,您可以隔离第三方依赖项并使用Adapter或Bridge模式从那里调用第三方库。我相信这将是一个充分的解耦解决方案。
但是,您的意图似乎是删除 import
,因此依赖?
ThirdParty
类上实施反射? Java与面向反射的编程兼容。这使您可以检查和检查类并在运行时动态调用它们的方法。它将消除import
类的依赖关系和ThirdParty
语句。
一般而言,使用Reflection,您必须找到该类并检查其方法。在这种情况下,我假设doAction()
方法来自ThirdPartyClass
。
// import ThirdPartyLibrary statement somewhere here
// Instantiating object with concrete class that implements methods from ThirdParty. From your code now, it would be "Builder".
ThirdParty<S> thirdPartyObject = new ThirdPartyImp<S>();
// Invoking doAction method which returns an S object
S foo = thirdPartyObject.doAction();
// Inspect the class finding it using its path and instantiating an object
ThirdParty<S> thirdPartyObject = Class.forName("classpath.to.ThirdPartyImp").newInstance(); // Using a concrete class to instantiate.
// Finding the doAction method. This is assuming we have knowledge that a method with this name exists. Reflection could go as deep as not knowing the methods and having some control structure inspecting them.
Method doAction = thirdPartyObject.getClass().getDeclaredMethod("doAction", new Class<?>[0]);
// Do action is invoked and it returns an object S.
S foo = thirdPartyObject.invoke(thirdPartyObject);