当方法递归时如何重构以避免循环依赖?
我已经编写了代码将对象转换为所需的类型。如果所需的类型是自定义类对象,并且它有另一个对象,我们需要递归地转换它。顺便说一句:如果输入是 hashMap,我就会知道我需要构造一个对象。如果在 HashMap 中存在另一个 hashMap,那么我需要了解它的对象位于对象内部。我需要从内部 hashMap 构建内部对象。为了构建它,我将递归调用该方法。我在这里描述了代码。但是 Castor 和 MyBuilder 这两个类都变成了循环。我不知道如何打破它。如果该方法不是循环的,我们就可以打破依赖性。但是对于循环,有人可以提供帮助吗? 我可以引入任何模式或者如何重构它?
提前致谢。
代码是这样的: 为了更快地访问循环 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);
}
} }
I have written code to cast the object to the required type. And if the required type is a custom class object and it has another object we need to cast it recursively. BTW: I will know that I need to construct an object if the input is a hashMap. If in side a HashMap if another hashMap is there then I need to understand that its object inside an object. And inside object I need to build from the inner 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. 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.....
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一部分对我来说似乎很可疑 - 您正在迭代每个方法,并且对于您调用的每个方法 setParameterToEntity
您忽略散列映射中的键并仅传递值(请参阅 setParameterToEntity sig)
您将相同的对象传递给您正在构建的类中的每个方法。
我假设您的哈希映射是 setter 名称和关联值的列表,因此您不应该迭代哈希映射键并为每个键标识方法,然后使用哈希映射中的值作为参数调用该方法。为了支持多个参数,您需要另一个哈希图。
由于此代码迭代哈希图中的对象图,并生成另一个对象图,因此只有当哈希图足够聪明,可以存储对哈希图中其他元素的引用时,才会出现循环,或者您在代码中出现错误,例如 a) 传入相同的对象而不是对象中的元素(导致其旋转),或者b)您有一个错误,您正在迭代正在构建的对象而不是哈希图。
鉴于上面的代码片段,我认为问题都是:),因此迭代哈希图而不是我上面所说的声明的方法,并确保将方法(根据哈希图键确定)和值从哈希图传递到setParameterToEntity,而不是哈希图本身。
This section seems suspect to me - you are iterating over each method and for each one you are invoking setParameterToEntity
You are ignoring the key in the hash map and just passing in the value (see setParameterToEntity sig)
You pass the same object in to each and every method in the class you are building .
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. To support multiple parameters you would need another 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.
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.