there is a class named as “composite”, in which, it stores a list. I want to write a method to calculate the result of the list. where should I wirte the calculation method? Based on the OO methodology, do I need to create an interface of calculation, and make it be implemented by the composite class? If yes, can you explain why?
Thanks.
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()); } }
It almost sounds like you have your OO a bit mixed up. If I had a class called composite it could implement an interface which had a method called calculate. Then by requirement of the interface your class needs a calculate method. Also, you tagged database-design for this. Was there something more to your question or was this just a generic OOP question?