我为这项任务编写了这段代码,我希望它能够成为一个很好的因素。基本上,这是一个简单的老式计算器的一部分,它执行加法,减法,乘法,除法(当执行除法时,应始终显示提醒)。我们需要为每个操作设置单独的类(加法,减法,乘法,除法,但我已经引入了一个 - 提醒)。您是否有任何建议,或者您是否认为我对Java泛型概念的理解存在差距?
public class Logic
implements LogicInterface {
private final int ADDITION = 1;
private final int SUBTRACTION = 2;
private final int MULTIPLICATION = 3;
private final int DIVISION = 4;
/**
* Reference to the Activity output.
*/
protected ActivityInterface mOut;
/**
* Constructor initializes the field.
*/
public Logic(ActivityInterface out){
mOut = out;
}
/**
* Perform the @a operation on @a argumentOne and @a argumentTwo.
*/
public void process(int argumentOne,
int argumentTwo,
int operation){
OperationsInterface operationsInterface =null;
if(operation==ADDITION)
{
operationsInterface = new Add();
}
else if(operation==SUBTRACTION)
{
operationsInterface = new Subtract();
}
else if(operation==MULTIPLICATION)
{
operationsInterface = new Multiply();
}
else
{
operationsInterface = new Divide();
}
if(argumentTwo==0 && operation == DIVISION) {
mOut.print("You cannot divide by zero!");
}
else {
try {
//get the result
int result = operationsInterface.process(argumentOne, argumentTwo);
mOut.print(String.valueOf(result));
//add the reminder to the output in case we are performing division
if (operation == DIVISION) {
operationsInterface = new Reminder();
mOut.print(result + " R: " + String.valueOf(operationsInterface.process(argumentOne, argumentTwo)));
}
}
catch (Exception exception)
{
mOut.print("Something went wrong!");
}
}
}
}
答案 0 :(得分:0)
我不知道这与仿制药有什么关系。
从代码审查的角度来看:
Add
类,一个进行简单的添加,另一个进行打印。如果你想要交换它们,你需要更改new Add()
部分,但是你不能有两个计算器,一个用简单的Add,另一个用扩展的。因此,优良作法是在某种可以轻易覆盖的工厂方法中使用new
,例如protected OperationInterface createAdd() {return new Add();}
。然后,您可以将计算器子类化并覆盖createAdd()
。当然,同样适用于所有其他运营商。OperationInterface
似乎返回int
。我不认为它适用于分裂。应该至少是double
。Reminder
视为Divide
的子类。至少逻辑只与除法运算有关,因此应该位于Divide
类或它的某个子类中。答案 1 :(得分:0)
以下内容可能会让您了解如何重构您的课程设计:
定义如下所示的界面:
public interface LogicInterface<T extends Number> {
T calculate(T operand1, T operand2);
}
为您的操作实施此界面:
public class Addition implements LogicInterface<Integer> {
public Integer calculate(Integer operand1, Integer operand2) {
return operand1.intValue() + operand2.intValue();
}
}
或
public class Division implements LogicInterface<Integer> {
public Integer calculate(Integer operand1, Integer operand2) {
if (operand2 == null) throw new IllegalArgumentException();
return operand1.intValue() / operand2.intValue();
}
}
等
实施工厂:
public class CalculatorFactory {
public enum CalculatorType {
ADD, SUBTRACT, MULTIPLY, DIVIDE, MODULO; // etc
}
public static LogicInterface<Integer> getOperator(CalculatorType type) {
switch (type) {
case ADD: return new Addition();
case DIVIDE: return new Division();
// etc
default: throw new UnsupportedOperationException("Operation type not supported");
}
}
}
用户如下:
public class CalculatorTest {
public static void main(String[] args) {
LogicInterface<Integer> add = CalculatorFactory.getOperator(CalculatorType.ADD);
System.out.println("Sum of 1 and 2: " + add.calculate(14, 16));
}
}
因此,您只需根据需要实现界面即可添加更多运算符,只需更改工厂类即可。其余的不应该改变。
希望它能让您了解如何实施。