I have a few classes which represent arithmetic operations (Plus, Minus, Pow...) in Java which extend the same abstract class Operator
but they differ in one method -calculate
.
I'm trying to find a way to avoid the switch-case conditions in order to implement these classes in the right way using design patterns (new to design patterns), but how do I do it? Or is the switch-case statement is the right way to implement it?
here is the abstract class:
public abstract class Operator {
private int a, b;
public Operator(int a, int b){
this.a = a;
this.b = b;
}
public float calculate() {
// here I want to return the result depending on the operator. If Plus extends Operator then the returned value is this.a + this.b
}
public void print() {
System.out.println("This is the result : %f", this.calculate());
}
}
Aucun commentaire:
Enregistrer un commentaire