简体   繁体   English

如何在没有instanceof的情况下“映射”两个不同的对象继承树

[英]How to "map" two different object inheritance trees without instanceof

I have a design question I can't get a good solution for.我有一个设计问题,我找不到好的解决方案。 This is my problem:这是我的问题:

There are two different object "trees" which need to processed together.有两个不同的对象“树”需要一起处理。 Object tree one:对象树一:

  • AbstractObjectTreeOne with Sub1ObjectTreeOne and Sub2ObjectTreeOne AbstractObjectTreeOneSub1ObjectTreeOneSub2ObjectTreeOne
  • AbstractObjectTreeTwo with Sub1ObjectTreeTwo and Sub2ObjectTreeTwo AbstractObjectTreeTwoSub1ObjectTreeTwoSub2ObjectTreeTwo

I now have a method where I get a list of AbstractObjectTreeOne and a list of AbstractObjectTreeTwo.我现在有一个方法可以获取 AbstractObjectTreeOne 的列表和 AbstractObjectTreeTwo 的列表。 They are exact the same size and "match" to each other by name.它们的大小完全相同,并且在名称上彼此“匹配”。 So I can loop through the objects in the list of AbstractObjectTreeOne and get the according AbstractObjectTreeTwo by name.所以我可以遍历 AbstractObjectTreeOne 列表中的对象,并按名称获取相应的 AbstractObjectTreeTwo。

Now it should be validated if the "matching" objects (by name) really match to each other, so the current code contains a lot of instanceof stuff.现在应该验证“匹配”对象(按名称)是否真的彼此匹配,因此当前代码包含很多 instanceof 内容。 Example:例子:

if (!(objectOfAbstractObjectTreeOne instanceof Sub1ObjectTreeOne)) {
   throw exception;
}

and then also in the same method然后也用同样的方法

if (!(objectOfAbstractObjectTreeTwo instanceof Sub1ObjectTreeTwo)) {
   throw exception;
}

After that, both parameters are cast to their "real" subtype to be further processed.之后,两个参数都被转换为它们的“真实”子类型以进行进一步处理。 This also does not feel very good.这也感觉不是很好。

This all feels not very object-oriented, but I currently do not have a good idea how to solve this.这一切都感觉不是很面向对象,但我目前不知道如何解决这个问题。 I tried the visitor pattern, but it only solves the instanceof issue in either AbstractObjectTreeOne or AbstractObjectTreeTwo and still contains a lot of instanceof.我尝试了访问者模式,但它只解决了 AbstractObjectTreeOne 或 AbstractObjectTreeTwo 中的 instanceof 问题,并且仍然包含很多 instanceof。

Maybe some of you have a good idea about this kind of problem.也许你们中的一些人对这类问题有一个好主意。 Maybe it's easy to solve, but I do not have the right idea yet.也许它很容易解决,但我还没有正确的想法。

This is called OOO Principle Polymorfism .这称为 OOO 原理 多态性

No need to use instanceof .无需使用instanceof You have to create an interface and use it in the declaration of the tree.您必须创建一个interface并在树的声明中使用它。 All subtypes should implement this interface, and you can call the required methods without typecasting.所有子类型都应该实现这个接口,你可以调用所需的方法而不需要类型转换。

This is an example.这是一个例子。

public interface ObjectTreeOne { void payloadOne() {}   }
public class Sub1ObjectTreeOne implements ObjectTreeOne {   void payloadOne() {}    }
public class Sub2ObjectTreeOne implements ObjectTreeOne {   void payloadOne() {}    }

List<ObjectTreeOne> objectTreeOne = new ArrayList<>();
objectTreeOne.add(new Sub1ObjectTreeOne()); 
objectTreeOne.add(new Sub2ObjectTreeOne());

public interface ObjectTreeTwo {    void payloadTwo() {}    }
public class Sub1ObjectTreeTwo implements ObjectTreeTwo {   void payloadTwo() {}    }
public class Sub2ObjectTreeTwo implements ObjectTreeTwo {   void payloadTwo() {}    }

List<ObjectTreeTwo> objectTreeTwo = new ArrayList<>();
objectTreeTwo.add(new Sub1ObjectTreeTwo()); 
objectTreeTwo.add(new Sub2ObjectTreeTwo());

for(int i = 0; i < 2; i++) {
    ObjectTreeOne objectTreeOne = objectTreeOne.get(i);
    ObjectTreeTwo objectTreeTwo = objectTreeTwo.get(i);
    
    objectTreeOne.payloadOne();
    objectTreeTwo.payloadTwo();
    
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM