牢固的本金违规
以下代码违反了坚实的本金,有人知道我应该如何重构吗? 我找不到任何解决方案来重构此代码以遵循坚实的主体。
public class Calculator
{
public int Calculate(int a, int b, string operation)
{
int result = 0;
if (operation.Equals("add"))
{
result = a + b;
}
else if (operation.Equals("multiply"))
{
result = a * b;
}
else if (operation.Equals("devide"))
{
result = a / b;
}
else if (operation.Equals("subtract"))
{
result = a - b;
}
return result;
}
}
据我了解,这可能只是违反班级的单一责任。但是我找不到重构的方法。
The following code violates Solid principal, does anybody know how should I refactor it?
I couldn't find any solution to refactoring this code to follow solid principals.
public class Calculator
{
public int Calculate(int a, int b, string operation)
{
int result = 0;
if (operation.Equals("add"))
{
result = a + b;
}
else if (operation.Equals("multiply"))
{
result = a * b;
}
else if (operation.Equals("devide"))
{
result = a / b;
}
else if (operation.Equals("subtract"))
{
result = a - b;
}
return result;
}
}
As I understand it can be just single responsibility which the class violated it. but I didn't find a way to refactoring it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜问题是您有一个单个函数执行许多计算,因此这样做不再简单,并且不仅仅是一个责任。
没有替换或扩展的方法,这通常来自使用实现固定接口的类,然后根据需要创建一个实例。
从其可以执行的计算中隔离的计算类别都不是。
如果您希望添加另一种计算类型,例如
Math.pow(a,b)
您需要更改伸展switch> switch
语句的函数,从而更改您的计算
类。如果您可以拥有一个接口:
那么您有四个类
add
,减去
,乘数
,divide
。每个类都实现一个算术公式。您的计算器类可以实现
icalculation
类型实例的列表,例如list< icalculator>计算=新列表< calculator> = {new add(),new subtract};
,或者您可以将计算器注入施工时间支持的类型列表
例如
calculator.calculate
可以将索引输入列表中以选择计算类型或
calculator.calculate
可以更改以允许注入具有较小更改的 计算实例您可以扩展计算器类,以使其可以将计算串在一起。
您能看到随着进步的方式,
计算器
类似乎可以获得更多功能,而实际上对其可以执行的计算有任何了解?I guess that the problem is that you have a single function performing many calculations and in so doing it is no longer simple and has more than a single responsibility.
There is no means of substitution or extension, this typically comes from using classes that implement a fixed interface and then create an instance as you need it.
Neither is the Calculator class isolated from the calculations that it can perform.
If you wished to add another calculation type e.g.
Math.Pow(a,b)
you would need to change the function extending theswitch
statement, thus changing yourCalculation
class.If you could have a single interface:
then you have four classes
Add
,Subtract
,Multiply
,Divide
. Each class implements one arithmetic formula.Your Calculator class could implement a list of
ICalculation
type instances e.g.List<ICalculator> calculations = new List<Calculator>={new Add(), new Subtract};
or you could inject your Calculator with a list of types that it supports at construction time
e.g.
Calculator.Calculate
could take an index into the list to select the calculation typeor
Calculator.Calculate
can be altered to allow for injection of a calculation instance e.g.With a minor changes you could extend the Calculator class to allow it to string calculations together.
Can you see how as you progress the
Calculator
class appears to get more functionality without actually knowing anything about the calculations that it can perform?