2012-01-23 27 views
0

有一個名爲「composite」的類,其中存儲了一個列表。我想寫一個方法來計算列表的結果。我應該在哪裏使用計算方法?基於OO方法論,我是否需要創建一個計算接口,並使其由複合類實現?如果是的話,你能解釋爲什麼嗎? 謝謝。如何在OO的用戶界面

package composite; 

public abstract class Component { 

private int level; 

public abstract void add(Component component); 

public abstract void remove(Component component); 

public abstract void eachChild(); 

    public int getLevel() { 
     return level; } 

    public void setLevel(int level) { 
     this.level = level; } } 



package composite; 

import java.util.ArrayList; 

public class Composite extends Component implements ILevelCalculator 
{ 

ArrayList<Component> componentList = new ArrayList<Component>(); 

@Override public void add(Component component) { 
componentList.add(component); } 

@Override public void remove(Component component) { 
componentList.remove(component); } 

@Override public void eachChild() { 
System.out.println(this.getLevel() + " excuted..."); for 
(Component component : componentList) { 
     component.eachChild(); } } 

@Override public int getLevel() { int levelResult; levelResult 
= getTheBiggestNo(componentList); return levelResult; } 

private int getTheBiggestNo(ArrayList<Component> componentList) { 
int theBiggestNo = 0; if (componentList.isEmpty()) { 
     System.out.println("The list is empty!"); } else { 
     theBiggestNo = getCompareResult(componentList); } return theBiggestNo; } 

private int getCompareResult(ArrayList<Component> componentList) { 
int result = 0; for (Component biggest : componentList) { 
     if (biggest.getLevel() > result) { 
     result = biggest.getLevel(); 
     } } return result; } } 


package composite; 

public interface ILevelCalculator { public int getLevel(); } 


package composite; 

public class Leaf extends Component { 

    @Override public void add(Component component) { 
     throw new UnsupportedOperationException("Not supported yet."); } 

    @Override public void remove(Component component) { 
     throw new UnsupportedOperationException("Not supported yet."); } 

    @Override public void eachChild() { 
     System.out.println("Leaf's name:"+this.getLevel()); } 

} 



package composite; 

public class Main { 

public static void main(String[] args) { Composite questionnaire 
= new Composite(); for(int i=0;i<4;i++){ 
     Composite dimension = new Composite(); for(int j=0;j<5;j++){ 
     Leaf question = new Leaf(); 
     question.setLevel(i); 
     dimension.add(question); } 
     questionnaire.add(dimension); 
     System.out.println("The dimension < "+i+" > result is: "+questionnaire.getLevel()); } 

    System.out.println("The final result is: 
"+questionnaire.getLevel()); } } 
+3

嘗試發佈一些代碼或使問題更清楚一點,因爲現在很難理解所講述的內容。 – Egor

+1

請詳細說明Q –

回答

0

它幾乎聽起來像你有你的OO有點混。如果我有一個名爲composite的類,它可以實現一個名爲calculate的方法。然後按照接口的要求,你的類需要一個計算方法。此外,您爲此標記了數據庫設計。有沒有更多的問題或者這只是一個普通的面向對象問題?

+0

嗨,夥計,我只是附上了代碼,請你幫忙?我想知道創建界面是否正確? – loql

+0

我認爲你在正確的軌道上。我想你實際上是想從你在這裏定義的方式來實現組件類的接口。複合意味着多態使用?什麼是你想要完成的英文版本?業務問題?有時它可以幫助我先定義,然後編寫類/接口代碼。 –

+0

這裏還有最後一件事。目前還不清楚你是否真的需要一個界面。你有其他的類實現這個接口嗎?如果僅僅是這個單獨的類,你甚至可能不需要繼承。 –