dimanche 15 septembre 2019

Composite Design pattern - how to create calculator

I would to experiement between 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:

enter image description here

Keeping this class diagram in mind this is what i have so far in java but i don't mind psuedo-code:

lets 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()
}

lets now define  composite:

public CompositeOperand:ArithmeticExpression{

private List operandList = new ArrayList(); //now what ???
//here im a little lost what i should do ? can you help me ? }

in the composite should i be checking what exactly ? obviously i need to some how know if its an operator or integer here but i dont know exaclty how to put it together.

Aucun commentaire:

Enregistrer un commentaire