Step1 ~ Step2:
Calculator
클래스 내에서 switch 문을 사용하여 사칙연산 구현.
Step3:
각 연산(AddOperation
, SubstractOperation
등) 별도 클래스로 분리. Calculator
클래스는 해당 연산 클래스들을 주입 받아 사용.
Step4:
Calculator
클래스는 단일 AbstractOperation
객체만을 주입 받아 사용. 연산 변경을 위해 setter 메서드 사용.
Main.java
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
System.out.println(calculator.calculate("*",13,20));
System.out.println(calculator.calculate("/",13,20));
System.out.println(calculator.calculate("-",13,20));
System.out.println(calculator.calculate("+",13,20));
System.out.println(calculator.calculate("%",13,20));
}
}
Calculator.java
public class Calculator {
double calculate(String sign,int num1,int num2){
AbstractOperation re;
switch (sign) {
case "+":
return num1+num2;
case "-":
return num1-num2;
case "/":
return num1/num2;
case "*":
return num1*num2;
case "%":
return num1%num2;
}
return 0;
}
}
Main.java
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator(new AddOperation(),new SubstractOperation(),
new MultiplyOperation(),new DivideOperation());
System.out.println(calculator.calculate("*",13,20));
System.out.println(calculator.calculate("/",13,20));
System.out.println(calculator.calculate("-",13,20));
System.out.println(calculator.calculate("+",13,20));
System.out.println(calculator.calculate("%",13,20));
}
}
Calculator.java
public class Calculator {
AddOperation addOperation;
SubstractOperation substractOperation;
MultiplyOperation multiplyOperation;
DivideOperation divideOperation;
Calculator(AddOperation addOperation,SubstractOperation substractOperation,
MultiplyOperation multiplyOperation, DivideOperation divideOperation){
this.addOperation=addOperation;
this.substractOperation=substractOperation;
this.multiplyOperation=multiplyOperation;
this.divideOperation=divideOperation;
}
double calculate(String sign,int num1,int num2){
AbstractOperation re;
switch (sign) {
case "+":
return addOperation.operate(num1,num2);
case "-":
return substractOperation.operate(num1,num2);
case "/":
return divideOperation.operate(num1,num2);
case "*":
return multiplyOperation.operate(num1,num2);
case "%":
return num1%num2;
}
return 0;
}
}
Main.java
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator(new AddOperation());
System.out.println(calculator.calculate(13,20));
calculator.setter(new SubstractOperation());
System.out.println(calculator.calculate(13,20));
calculator.setter((new DivideOperation()));
System.out.println(calculator.calculate(13,20));
calculator.setter(new MultiplyOperation());
System.out.println(calculator.calculate(13,20));
}
}
Calculator.java
public class Calculator {
AbstractOperation abstractOperation;
Calculator(AbstractOperation abstractOperation){
this.abstractOperation = abstractOperation;
}
public void setter(AbstractOperation abstractOperation){
this.abstractOperation = abstractOperation;
}
double calculate(int num1,int num2){
return abstractOperation.operate(num1,num2);
}
}