2011-08-30 31 views
2

我有两个类中删除循环依赖是其如下:如何从两类或具体类型

public class A{ 
private String id ; 
private SortedMap<String,B> answer = new TreeMap<String,B>(); 
private String text; 
} 

public class B{ 
private String id = null ; 
private SortedMap<String,A> question = new TreeMap<String,A>(); 
private String text = null; 
} 

有什么办法,我可以从上面的类中删除循环依赖..?

+2

你为什么想这样做? (这可能有助于提供一些想法) – Touko

+0

您可以在http://stackoverflow.com/questions/7231993/how-can-i-parse-the-following-xml-using-jdom问题中看到XML结构,并且我想要解析结构并创建一些数据结构,该数据结构保存xml结构.. –

+0

建议任何想法我应该怎么做? –

回答

1

不,除非你删除其中一张地图。

+0

我无法删除任何地图... –

0
public class A{ 
private String id ; 
private SortedMap<String,A> answer; 
private String text; 
} 
public class B extends A{ 
} 

,如果你觉得你需要,或只使用一个布尔字段在一个

+0

不错,但不清楚'B'是否是来自该示例的'A'。 (downvote不是我的顺便说一句) – Bozho

+0

加上,他有一个'地图'在A级之前。 – Thilo

3

没有没有,但是那也没问题。

在JAVA中有循环依赖没有问题。如果你想要在两个方向上穿越结构,那就很常见了。想一想父母和孩子彼此都知道的树,从而创建一个循环依赖。

垃圾收集器将检测到循环依赖关系,并处理这很好。在这两个构造函数,这将导致堆栈溢出:)

+0

“垃圾收集器将处理得很好。”编译器也一样。憎恨那些你必须用其他语言进行的前向声明。 – Thilo

+0

循环依赖关系不是不好的做法吗? – flash

+2

如果你逻辑上有一个循环依赖,那么它并不坏。用循环依赖来设计是不好的,但是根据给出的信息,我们无法判断它是否是不好的设计。 – Bozho

0

回顾前面的问题有循环依赖时发生

唯一的问题 - 你可以改变XML模式,并添加<nextquestion>标签的某种到答案。然后,对应的XML文件是:

<decision> 
    <question id="0"> 
    <questionText>What type is your OS?</questionText> 
    <answer id="0"> 
     <answerText>windows</answerText> 
    </answer> 
    <answer id="1"> 
     <answerText>linux</answerText> 
    </answer> 
    <answer id="2"> 
     <answerText>mac</answerText> 
    </answer> 
    </question> 
    <question id="1"> 
    <questionText>What are you looking for?</questionText> 
    <answer id="0"> 
     <answerText>table</answerText> 
     <!-- NEW TAG HERE --> 
     <nextquestion refid="3" /> 
    </answer> 
    <answer id="1"> 
     <answerText>chair</answerText> 
    </answer> 
    <answer id="2"> 
     <answerText>bed</answerText> 
    </answer> 
    <answer id="3"> 
     <answerText>cloth</answerText> 
    </answer> 
    </question> 
    <!-- ALL QUESTIONS ARE CHILDREN OF ROOT WITH UNIQUE ID --> 
    <question id="3"> 
    <questionText>Which color table you want?</questionText> 
    <answer id="0"> 
     <answerText>green</answerText> 
    </answer> 
    <answer id="1"> 
     <answerText>black</answerText> 
    </answer> 
    <answer id="2"> 
     <answerText>pink</answerText> 
    </answer> 
    </question> 
</decision> 

您可能需要使用唯一ID的答案太或者即使你想重用针对不同问题的答案(再次反应堆模型许多一对多关系)

你的类:

public class Question { 
    private int id; 
    private String text; 
    private Set<Answer> answers; 
    // ... 
} 

public class Answer { 
    private int id; 
    private String text; 
    private Question nextQuestion; 
} 

当然有一个循环依赖,但这是绝对通缉,从模型真实领域继承

1

根据你有什么,你不需要两个类。尽量让课程更通用,你只需要一个。

public class AB { 
    private final String id ; 
    private final SortedMap<String,AB> answer = new TreeMap<String,AB>(); 
    private final String text; 
    private final boolean isA; // if you need to know if its an A or B. 
} 
0

尝试类似:

import java.util.*; 
class Question { 
    Question(int id, String question) { 
     this.id = id; 
     this.question = question; 
    } 
    static void toString(Question question, StringBuffer sb, int indent) { 
     for(int i=0;i<indent;i++) 
      sb.append('\t'); 
     sb.append(question.id).append(' ').append(question.question).append('\n'); 
     for (Map.Entry<Integer, Answer> entry : question.answers.entrySet()) { 
      Answer answer = entry.getValue(); 
      for(int i=0;i<=indent;i++) 
       sb.append('\t'); 
      sb.append(entry.getKey()).append(' ').append(answer.answer).append('\n'); 
      if (answer.question != null) { 
       toString(answer.question, sb, indent+2); 
      } 
     } 
    } 
    public String toString() { 
     StringBuffer sb = new StringBuffer(); 
     toString(this,sb, 0); 
     return sb.toString(); 
    } 
    int id; 
    String question; 
    SortedMap<Integer, Answer> answers = new TreeMap<Integer, Answer>(); 
} 
class Answer { 
    Answer(int id, String answer) { 
     this.id = id; 
     this.answer = answer; 
    } 
    final int id; 
    final String answer; 
    Question question; // may be null 
} 
public class Main { 
    public static void main(String[] args) { 
     Question q0 = new Question(0, "What are you looking for?"); 
     Answer a0 = new Answer(0, "table"); 
     q0.answers.put(a0.id, a0); 
     a0.question = new Question(0, "Which color table you want?"); 
     a0.question.answers.put(0, new Answer(0, "green")); 
     System.out.println(q0); 
    } 
} 
1

如果您有相互引用的两个班,javac的会解决,如果你在同一时间编译两个:

file: dev/A.java 
class A { 
    B b = null; 
    public A(B b) 
    { 
     this.b = b; 
    } 
}; 

file: dev/B.java 
package dev; 
class B { 
    A a = null; 
    public B(A a) 
    { 
     this.a = a; 
    } 
}; 

$ javac -d classes dev/A.java 
dev/A.java:3: cannot find symbol 
symbol : class B 
location: class dev.A 
    B b = null; 
^ 
dev/A.java:4: cannot find symbol 
symbol : class B 
location: class dev.A 
    public A(B b) 
      ^
2 errors 
$ javac -d classes dev/B.java 
dev/B.java:3: cannot find symbol 
symbol : class A 
location: class dev.B 
    A a = null; 
    ^
dev/B.java:4: cannot find symbol 
symbol : class A 
location: class dev.B 
    public B(A a) 
     ^
2 errors` 

但如果你输入:

$ javac -d classes dev/A.java dev/B.java 

它将解析循环编译器依赖项。

1

(这其实是一个评论,但我没有足够的声誉分,要做到这一点)

: - >为什么你会喜欢做的事是什么?
,因为Findbugs在Pattern中这么说:CD_CIRCULAR_DEPENDENCY: 该类对其他类具有循环依赖性。这使得构建这些类很困难,因为每个类都依赖于另一个来正确构建。考虑使用接口来打破硬依赖。

wikipedia说: ...在较大的软件模块之间的软件设计循环依赖 的,因为他们的负面影响被认为是一个反模式... 循环的依赖往往是由经验的程序员介绍...