简体   繁体   English

当方法递归时如何重构以避免循环依赖?

[英]How to refactor to avoid cyclic dependencies when methods are recursive?

I have written code to cast the object to the required type.我已经编写了将 object 转换为所需类型的代码。 And if the required type is a custom class object and it has another object we need to cast it recursively.如果所需的类型是自定义 class object 并且它有另一个 object 我们需要递归地转换它。 BTW: I will know that I need to construct an object if the input is a hashMap.顺便说一句:如果输入是 hashMap,我会知道我需要构造一个 object。 If in side a HashMap if another hashMap is there then I need to understand that its object inside an object.如果在 HashMap 旁边,如果另一个 hashMap 在那里,那么我需要了解它的 object 在 ZA8CFDE6331BD59EB2ACZF46 中。 And inside object I need to build from the inner hashMap.在 object 内部,我需要从内部 hashMap 构建。 To build it I will call the method recursively.为了构建它,我将递归调用该方法。 Code I have depicted here.我在这里描述的代码。 But these 2 classes Castor and MyBuilder both have become in cycles.但是这 2 个类 Castor 和 MyBuilder 都变成了循环。 I am not getting how to break it.我不知道如何打破它。 If the method is not cyclic we can break dependecy.如果方法不是循环的,我们可以打破依赖。 But with cycles can any one help?但是对于循环,任何人都可以提供帮助吗? Any pattern can I introduce or how can I refactor this?我可以引入任何模式或如何重构它?

Thanks in advance.提前致谢。

Code is something like this: For faster access to cycle pl refer to: returnValue castPrimitive(.... and void setParameterToEntity.....代码是这样的:为了更快地访问循环 pl,请参阅:returnValue castPrimitive(.... and void setParameterToEntity.....

MyBuilder myBuilder = new MyBuilder();

class Castor {
public Object castToRequiredType( Type type, Object object) {
    String typeString = type.toString();
    Object returnValue = null;
    if (myUtils.isTypePrimitive(typeString)) {
        returnValue = castPrimitive(object.toString(), typeString);
    }else if {
 // some conditions and some casting logic.
}
else {
        returnValue = myBuilder.buildCustomObject(this,typeString, object);
    }
    return returnValue;
   }
  // other methods which call castToRequiredType() recursively.
}


 class MyBuilder{
 buildCustomObject(Castor castor,
          String classOfType, Object object){
     Class<?> loadedClass = myUtils.loadClass(classOfType);

        instance = loadedClass.newInstance();
        HashMap<?, ?> myMap;
        List<Method> declaredMethods = myUtils.getMethodsForClass(loadedClass);
        for (Method method : declaredMethods) {
            if (object instanceof HashMap<?, ?>) {
                myMap = (HashMap<?, ?>) object;
        // ITERATE THROUGH MAP AND CALL THE SET PARAMETER TO ENTITY METHOD.
                    }
                }
            }
     return instance;
}

 void setParameterToEntity(Castor caster,
Object instance,  Method method,  Object value) {
    ype[] parameterTypes = method.getGenericParameterTypes();
    Object castedValue = caster.castToRequiredType(
    parameterTypes[0], value);          
   }

} } } }

This section seems suspect to me - you are iterating over each method and for each one you are invoking setParameterToEntity这部分对我来说似乎很可疑 - 您正在迭代每个方法,并且对于您正在调用的每个方法 setParameterToEntity

for (Method method : declaredMethods) {
        if (object instanceof HashMap<?, ?>) {
            myMap = (HashMap<?, ?>) object;
    // ITERATE THROUGH MAP AND CALL THE SET PARAMETER TO ENTITY METHOD.
  1. You are ignoring the key in the hash map and just passing in the value (see setParameterToEntity sig)您忽略了 hash map 中的键并仅传入值(请参阅 setParameterToEntity sig)

  2. You pass the same object in to each and every method in the class you are building.您将相同的 object 传递给您正在构建的 class 中的每个方法。

I assume your hashmap is a list of setter names and the associated value, so shouldn't you be iterating over the hashmap keys and for each one identify the Method, and then invoke the Method with the value from the hashmap as the parameter.我假设您的 hashmap 是设置器名称和相关值的列表,因此您不应该遍历 hashmap 键并为每个键标识方法,然后使用 ZDDA7806A4847EC61B5940C26 中的值调用方法。 To support multiple parameters you would need another hashmap.要支持多个参数,您需要另一个 hashmap。

Since this code iterates over an object graph in hashmaps, and produces another object graph, cycles should appear only if the hashmaps are clever enough to store references to other elements within the hashmap, or you have a mistake in the code like a) passing in the same object instead of elements from the object (causing it to spin), or b) you have a bug where you are iterating over the object being built rather the hashmap graph.由于此代码迭代哈希图中的 object 图,并生成另一个 object 图,因此只有当哈希图足够聪明以存储对 ZDDA7806A4847EC61B5940C2623A5AABD 中其他元素的引用时,才会出现循环the same object instead of elements from the object (causing it to spin), or b) you have a bug where you are iterating over the object being built rather the hashmap graph.

Given the code snippet above, I think the problem is both:), so iterate over the hashmap not the declaredMethods as I said above, and make sure you pass the Method (determined from the hashmap key), and value from the hashmap to the setParameterToEntity, not the hashmap itself.鉴于上面的代码片段,我认为问题出在这两个方面setParameterToEntity,而不是 hashmap 本身。

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

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