牢固的本金违规

发布于 2025-02-06 05:26:36 字数 636 浏览 1 评论 0原文

以下代码违反了坚实的本金,有人知道我应该如何重构吗? 我找不到任何解决方案来重构此代码以遵循坚实的主体。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2025-02-13 05:26:36

我猜问题是您有一个单个函数执行许多计算,因此这样做不再简单,并且不仅仅是一个责任。

没有替换或扩展的方法,这通常来自使用实现固定接口的类,然后根据需要创建一个实例。

从其可以执行的计算中隔离的计算类别都不是。

如果您希望添加另一种计算类型,例如Math.pow(a,b)您需要更改伸展switch> switch语句的函数,从而更改您的计算类。

如果您可以拥有一个接口:

public interface ICalculation
{
    public int Calculate(int a, int b);
}

那么您有四个类add减去乘数divide。每个类都实现一个算术公式。

您的计算器类可以实现icalculation类型实例的列表,例如
list< icalculator>计算=新列表< calculator> = {new add(),new subtract};

,或者您可以将计算器注入施工时间支持的类型列表
例如

class Calculator
{
    private List<ICalculator> _calculations;
    Calculator(List<ICalculator> calculations)
    {
        _calculations = calculations;
    }
}

calculator.calculate可以将索引输入列表中以选择计算类型

Calculate(uint index, int a, int b)
{
    //TODO: remember to bounds check index against _calculations.Count
    return _calculations[index].Calculate(a,b);
}

calculator.calculate可以更改以允许注入

Calculate(ICalculation calculation, int a, int b)
{
    //TODO: null check required
    return calculation.Calculate(a,b);
}

具有较小更改的 计算实例您可以扩展计算器类,以使其可以将计算串在一起。

您能看到随着进步的方式,计算器类似乎可以获得更多功能,而实际上对其可以执行的计算有任何了解?

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 the switch statement, thus changing your Calculation class.

If you could have a single interface:

public interface ICalculation
{
    public int Calculate(int a, int b);
}

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.

class Calculator
{
    private List<ICalculator> _calculations;
    Calculator(List<ICalculator> calculations)
    {
        _calculations = calculations;
    }
}

Calculator.Calculate could take an index into the list to select the calculation type

Calculate(uint index, int a, int b)
{
    //TODO: remember to bounds check index against _calculations.Count
    return _calculations[index].Calculate(a,b);
}

or Calculator.Calculate can be altered to allow for injection of a calculation instance e.g.

Calculate(ICalculation calculation, int a, int b)
{
    //TODO: null check required
    return calculation.Calculate(a,b);
}

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文