简体   繁体   English

我有一个抽象类和两个扩展,可以有一个通用的映射器吗?

[英]I have an abstract class and two extensions, can I have a common mapper?

In my project I have the following model: 在我的项目中,我有以下模型:

public class Object1 extends ObjectBase {
    private Value1 specific1;
}

public class Object2 extends ObjectBase {
    private Value2 specific2;
}

public abstract class ObjectBase {
    private String value1;
    private String value2;
    private String value3;
}

public class Source {
    private String value1;
    private String value2;
    private String value3;
}

I need to make a way of mapping a Source object into either of Object1 or Object2 . 我需要一种将Source对象映射到Object1Object2 Is it possible to have a common function for setting the ObjectBase values? 是否可以具有用于设置ObjectBase值的通用功能? Previously, this was done by creating a new instance of either, and then send it to something like the following: 以前,这是通过创建其中一个的新实例,然后将其发送到以下内容来完成的:

public static void convertToObjectBase(Source source, ObjectBase objectBase) {
    objectBase.setValue1(source.getValue1);
    objectBase.setValue2(source.getValue2);
    objectBase.setValue3(source.getValue3);
}

This latter way, editing in place, seems wrong to me. 后一种方式,进行适当的编辑,对我来说似乎是错误的。 Do you know a better way of doing this, without having to replicate all the setters for ObjectBase for each case? 您是否知道一种更好的方法,而不必为每种情况复制ObjectBase所有设置方法? There's something like 40 values that have to be mapped in each case, requiring some internal logic, but this would be the same for both Object1 and Object2 . 每种情况下都需要映射约40个值,这需要一些内部逻辑,但是Object1Object2都相同。

I'm thinking that generics could be the way to go here, but I'm not completely sure. 我认为仿制药可能是解决问题的方法,但我不确定。

Edit: I had some misleading value names, updated these. 编辑:我有一些误导的值名称,更新了这些。

You can try using instanceof operator in your mapper. 您可以尝试在映射器中使用instanceof运算符。 Something like 就像是

if (objectBase instanceof Object1) {
        //do something 
}
else if (objectBase instanceof Object2) {
        //do something else
}

. . .

This way you can map whatever properties you want to map for child class using same mapper. 这样,您可以使用相同的映射器映射要为子类映射的任何属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在Java中的同一个类中没有抽象和静态方法 - Why can I not have an abstract and static method in the same class in Java 我可以在抽象类中有私有的final字段吗 - Can I have private final fields in abstract class 如何在两个方法之间使用一个公共变量? - How can I have a common variable between two methods? 在Spring 3中,我可以实例化将建议使用抽象方法的抽象类bean吗? - In Spring 3, can I instantiate an abstract class bean that will have abstract methods advised? 抽象类可以有构造函数吗? - Can an abstract class have a constructor? 如果我可以在创建者 class 中拥有多个工厂方法,为什么我还需要抽象工厂模式? - If I can have multiple factory methods in a creator class, why would I ever need the abstract factory pattern? 我可以在java中使用方法链接而不执行不安全操作的抽象构建器类吗? - Can I have an abstract builder class in java with method chaining without doing unsafe operations? 我可以在一个班级里有两个以上的学生吗? - can I have more than two finally block in a class 我应该如何解释接口和抽象类之间的区别? - How should I have explained the difference between an Interface and an Abstract class? 如何检查两个字符串是否具有相同的字母,但只打印一次常见的字母? - How can I check if two strings have the same letters, but only print the common letters once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM