2011-08-30 51 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說: ...在較大的軟件模塊之間的軟件設計循環依賴 的,因爲他們的負面影響被認爲是一個反模式... 循環的依賴往往是由經驗的程序員介紹...