在Java中进行对象到对象转换的简单/优雅方法?

在Java中进行对象到对象转换的简单/优雅方法?,java,Java,我必须接管并改进/完成一些将Java对象从第三方库转换为内部对象的代码。目前,这是通过一个大的if-else语句来实现的,该语句大致如下: if (obj instanceOf X) { //code to initialize internal object } else if (obj instanceOf Y) { //code to initialize different object } else if (obj instanceOf Z) { //more

我必须接管并改进/完成一些将Java对象从第三方库转换为内部对象的代码。目前,这是通过一个大的if-else语句来实现的,该语句大致如下:

if (obj instanceOf X)
{
    //code to initialize internal object
}
else if (obj instanceOf Y)
{
    //code to initialize different object
}
else if (obj instanceOf Z)
{
    //more init code
}
...

就我个人而言,我觉得这个解决方案并不令人满意;它既长又乱,更糟糕的是,许多if-else块包含更多的if-else块,用于处理子类和边缘情况。这个问题有更好的解决方案吗?

创建这样的接口

public interface Converter<S,T> {
  public T convert(S source);
}

并为X,Y,Z的每个对象实现它。然后将所有已知的转换器放到一张地图中,并获得快乐

创建这样的界面

public interface Converter<S,T> {
  public T convert(S source);
}

并为X,Y,Z的每个对象实现它。然后将所有已知的转换器放到一张地图中,并获得快乐

虽然它不适用于边缘情况,但在类和转换器之间构建映射

X.getClass->X转换器 Y.getClass->Y转换器


会让你更亲近。如果找不到叶类,您还需要检查超类。

虽然它不适用于边缘情况,但在类和转换器之间建立映射

X.getClass->X转换器 Y.getClass->Y转换器


会让你更亲近。如果找不到叶类,您还需要检查超类。

这样的代码及其所有instanceof条件都要求接口

您可能希望使用public void initialize方法创建一个可初始化的公共接口


然后,如果您的if-else只需解析为一个obj.initialize调用。

这样的代码及其所有instanceof条件都需要一个接口

您可能希望使用public void initialize方法创建一个可初始化的公共接口


然后,如果if-else只是解析为一个单独的obj.initialize调用。

如果这些内部对象向应用程序提供了一个接口,而不是直接使用,则调整它们,而不是转换它们

也就是说,如果你有这样的东西:

public class ThirdPartyClass { ... }
public interface InternalInterface { ... }
public class InternalClass { ... }

Internal foo(ThirdPartyClass thirdParty) {
    InternalClass internal = new InternalClass();
    // convert thirdPaty -> internal
    return internal;
}
public class ThirdPartyClass { ... }
public interface InternalInterface { ... }
public class InternalClass { ... }

public class ThirdPartyInternalAdapter implements InternalInterface {
    private final ThirdPartyClass thirdParty;
    public ThirdPartyInternalAdapter(ThirdPartyClass thirdParty) {
        this.thirdParty = thirdParty;
    }
    // implement interface in terms of thirdParty
}
然后改为这样做:

public class ThirdPartyClass { ... }
public interface InternalInterface { ... }
public class InternalClass { ... }

Internal foo(ThirdPartyClass thirdParty) {
    InternalClass internal = new InternalClass();
    // convert thirdPaty -> internal
    return internal;
}
public class ThirdPartyClass { ... }
public interface InternalInterface { ... }
public class InternalClass { ... }

public class ThirdPartyInternalAdapter implements InternalInterface {
    private final ThirdPartyClass thirdParty;
    public ThirdPartyInternalAdapter(ThirdPartyClass thirdParty) {
        this.thirdParty = thirdParty;
    }
    // implement interface in terms of thirdParty
}

您的问题不清楚这是否适用,但如果适用,这可能比直接对象到对象转换更容易、更有效。

如果这些内部对象为应用程序提供了接口,而不是直接使用,请调整它们,而不是转换它们

也就是说,如果你有这样的东西:

public class ThirdPartyClass { ... }
public interface InternalInterface { ... }
public class InternalClass { ... }

Internal foo(ThirdPartyClass thirdParty) {
    InternalClass internal = new InternalClass();
    // convert thirdPaty -> internal
    return internal;
}
public class ThirdPartyClass { ... }
public interface InternalInterface { ... }
public class InternalClass { ... }

public class ThirdPartyInternalAdapter implements InternalInterface {
    private final ThirdPartyClass thirdParty;
    public ThirdPartyInternalAdapter(ThirdPartyClass thirdParty) {
        this.thirdParty = thirdParty;
    }
    // implement interface in terms of thirdParty
}
然后改为这样做:

public class ThirdPartyClass { ... }
public interface InternalInterface { ... }
public class InternalClass { ... }

Internal foo(ThirdPartyClass thirdParty) {
    InternalClass internal = new InternalClass();
    // convert thirdPaty -> internal
    return internal;
}
public class ThirdPartyClass { ... }
public interface InternalInterface { ... }
public class InternalClass { ... }

public class ThirdPartyInternalAdapter implements InternalInterface {
    private final ThirdPartyClass thirdParty;
    public ThirdPartyInternalAdapter(ThirdPartyClass thirdParty) {
        this.thirdParty = thirdParty;
    }
    // implement interface in terms of thirdParty
}

您的问题不清楚这是否适用,但如果适用,这可能比直接对象到对象转换更容易、更有效。

可能,但没有更多关于您获取的对象以及基于它们的初始化类型的信息,很难说-这是一个取决于答案的答案。可能,但如果没有更多关于您获取的对象以及基于它们的初始化类型的信息,很难说-这是一个依赖于它的答案。这并没有真正的帮助,因为泛型类型是静态的,所以你仍然需要做大量的instanceof来使用正确的Convert实例。你把转换器放在一个由它的类类型键控的映射中。这并没有真正的帮助,因为泛型类型是静态的,所以你仍然需要做一些修改使用正确的Convert实例的instanceof。将转换器粘贴在一个由其类类型设置键控的映射中。