I want to explore the difference between recursion approach and composite design pattern. Composite design pattern reminds me of a tree structure. So if I have to write up how it would look in a class diagram, we could have this:
Keeping this class diagram in mind, here is what I have so far in Java; but I don't mind pseudo-code.
Let's create a leaf:
class NumericOperand extends ArithmeticExpression{
public Float add(String:s1,String:s2){
return s1.toFloat() + s2.toFloat()
}
public Float minus(String:s1,String:s2){
return s1.toFloat() - s2.toFloat()
}
public Float multiple(String:s1,String:s2){
return s1.toFloat() * s2.toFloat()
}
public Float divide(String:s1,String:s2){
return s1.toFloat() / s2.toFloat()
}
}
Let's now define composite:
public CompositeOperand extends ArithmeticExpression{
private List<NumericOperand> operandList = new ArrayList<NumericOperand>();
//now what ???
//here im a little lost what i should do ? can you help me ?
}
In the composite what should I be checking exactly? Obviously I need to somehow know if it's an operator or integer here, but I don't know exactly how to put it together.