了解单一责任原则 [英] Understanding the Single Responsibility Principle

查看:114
本文介绍了了解单一责任原则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于如何确定单个方法是否要完成一项职责,我感到很困惑,就像本书清洁代码中的以下代码一样

I am quite confused on how to determine if a single method has one responsibility being done just like the following code from the book Clean Code

public Money calculatePay(Employee e) throws InvalidEmployeeType {
        switch (e.type) {
            case COMMISSIONED:
                return calculateCommissionedPay(e);
            case HOURLY:
                return calculateHourlyPay(e);
            case SALARIED:
                return calculateSalariedPay(e);
            default:
                throw new InvalidEmployeeType(e.type);
        }
    }

正如作者在此代码段中所述:。 .. 显然要做的事情不止一件事。
第三,它违反了单一责任原则(SRP),因为有多个原因使它改变。
。乍一看代码,我在想该方法违反SRP的原因是,如果代码发生更改,则只有在增加雇员类型的情况下,它才是switch语句,但是当我尝试理解该方法时

As the author stated on this code snippet: "...clearly does more than one thing. Third, it violates the Single Responsibility Principle (SRP) because there is more than one reason for it to change.". On a first glance at the code I was thinking that how come that the method violates SRP since if there is a change in the code it would be the switch statement only if there will be an added employee type but as I try to understand the method further I came up with a hypothesis on why it violates the said principle.

我的假设是,由于该方法的名称为 calculatePay(Employee e),那么该方法的唯一职责就是支付计算,这是该方法的名称所暗示的,但是由于该方法内部有一个过滤Employee类型的开关,因此该过滤现在是另一种职责或另一种职责因此违反了SRP。我不知道我是否正确。

My hypothesis is that since the name of the method is calculatePay(Employee e) then the only responsibility of this method is for the payment computation as the method's name suggest but since inside the method there is a switch on filtering the type of Employee this filtering is now a different or another responsibility thus violates SRP. I don't know if I got it right.

推荐答案


...显然还有更多用途第三,它违反了
单一责任原则(SRP),因为
更改的原因不只一个。

"...clearly does more than one thing. Third, it violates the Single Responsibility Principle (SRP) because there is more than one reason for it to change."

这是您的答案。每次添加新的 Employee.Type 时,该方法都必须更改。另外,每个 Employee.Type 的计算也可能会更改。

There lies your answer. Every time a new Employee.Type is added that method will have to change. Also, the calculation for each Employee.Type might change too.

更好的解决方案是创建一个<员工及其拥有自己的 CalculatePay 抽象工厂 >。这样,当计算更改或添加新的 Employee.Type 时,只需更改一个类。

A better solution would be to create an Abstract Factory for Employee and each derivative of Employee having it's own implementation of CalculatePay. This way, only ONE class needs to changed when the Calculation changes or when a new Employee.Type is added.

这里是Clean Code的另一节摘录,其中有更详细的解释-

Here is another extract from Clean Code that explains in more detail -


此问题的解决方案(参见清单3-5)是:将switch
语句埋入抽象工厂的地下室9,切勿让
有人看到它。工厂将使用switch语句创建Employee派生类的
适当实例,并且各种
函数(例如calculatePay,isPayday和DeliveryPay)将通过雇员多态派发的
接口。我对switch语句的一般
规则是,如果它们
仅出现一次,用于创建多态对象,则可以容忍它们。

The solution to this problem (see Listing 3-5) is to bury the switch statement in the basement of an ABSTRACT FACTORY,9 and never let anyone see it. The factory will use the switch statement to create appropriate instances of the derivatives of Employee, and the various functions, such as calculatePay, isPayday, and deliverPay, will be dispatched polymorphically through the Employee interface. My general rule for switch statements is that they can be tolerated if they appear only once, are used to create polymorphic objects

这篇关于了解单一责任原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆