单一职责原则的实施

发布于 2024-07-11 06:41:54 字数 811 浏览 13 评论 0原文

如果我将我的对象分解为“单一职责”,是否有一个基本的想法,类似的对象应该生活在一起还是分开,例如,如果我有

class Employee_DataProvider() : IEmployee_DataProvider { ... };
class Employee_Details() : IEmployee_Details { ... };
class Employee_Payroll() : IPayroll() { ... };
class Employee_LeaveProcessing() : ILeaveProcessing_Client { ... };
...

所有这些生活在里面,但通过接口松散耦合的气味,一个拥有 Employee 类:

class Employee
{
    IEmployee_DataProvider _dataProvider;
    IEmployee_Details _details;
    IPayroll _payroll;
    ILeaveProcessing_Client _leaveProcessing;

    //My functions call the interfaces above

}

或者更多地考虑在代码中保持这些类完全独立(或至少尽可能独立)? 或者这两种方法都是 SRP 的有效用法吗?

编辑:我不想批评示例中给出的对象的可行性,我只是编造它来说明问题。 我同意数据、休假和工资处理不属于员工类别的范围。

SRP 确实要求我从作为现实世界表示的对象转向作为围绕单个功能概念的属性和方法的对象

If I break my Objects down to 'Single Responsibilities', is there a fundamental thought whether like objects should live together or separately, for example if I have

class Employee_DataProvider() : IEmployee_DataProvider { ... };
class Employee_Details() : IEmployee_Details { ... };
class Employee_Payroll() : IPayroll() { ... };
class Employee_LeaveProcessing() : ILeaveProcessing_Client { ... };
...

Is it bad smell to have all these living inside, yet loosely coupled to through interfaces, an owning Employee class:

class Employee
{
    IEmployee_DataProvider _dataProvider;
    IEmployee_Details _details;
    IPayroll _payroll;
    ILeaveProcessing_Client _leaveProcessing;

    //My functions call the interfaces above

}

or is the thinking more around keeping these classes completely separate (or as least as separate as is posible) in the code? Or are both these methods a valid usage of SRP?

EDIT: I do not want critique on the feasibility of the object given in the example, I just made it up to illustrate the question. I agree data, leave and payroll processing are not the domain of the employee class.

It does appear though that SRP is asking me to move away from the object as real world representation to object as a properties and methods around a single functional concept

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

甜警司 2024-07-18 06:41:54

回到 OOP 基础知识:Employee 对象应该具有反映它所做的事情的方法,而不是反映对其所做的事情。

Go back to OOP basics: the Employee object should have methods that reflect what it does, not what is done to it.

浅听莫相离 2024-07-18 06:41:54

尽管还没有人解决我关于原则本身的实际问题,而不是我给出的有点糟糕的例子,即 Employee 对象对影响它的过程了解太多,对于那些显然对这个问题感兴趣的人(有 2 “最喜欢的”星星)我已经做了更多的进一步阅读,尽管我希望有更多的讨论。

我认为当前的两个答案试图表达的是,分离的责任应该能够独立存在,而我的例子是不该做什么的完美例子。 我非常乐意接受这一点。

ObjectMentor(鲍勃叔叔的网站)中有一段段落,其中有一个示例将 Connection 和 DataReader 对象(以前位于调制解调器类中的两个对象,然后分离出来)合并到 ModemImplementation 中,但声明

但是,请注意我已经重新耦合
将两个职责合并为一个
Modem实现类。 这不是
理想的,但可能是必要的。
往往有一些原因,不得不做
与硬件的详细信息或
操作系统,迫使我们耦合事物
我们宁愿不结婚。 然而,
通过分离他们的接口我们有
解耦了这些概念
涉及应用程序的其余部分。

我们可以查看ModemImplementation
阶级是一种拼凑,或者是一种疣; 然而,
请注意,所有依赖项都会消失
从中。 没有人需要依赖这个
班级。 除了主要人员之外没有人需要
知道它存在。 因此,我们把
栅栏后面丑陋的部分。 它是
丑陋不必泄漏和污染
应用程序的其余部分。

我认为“注意所有依赖项都远离它”这一行在这里很重要

Although no one has yet addressed my actual question regarding the principle itself, rather than the somewhat poor example I gave, of an Employee object knowing too much about the processes that affect it, for those who are obviously interested in the question (there are 2 'favourites' stars) I've done a bit more further reading, although I was hoping for more of a discussion.

I think what the two current answers are trying to say is that responsibilities separated should be able to be stand alone, and that my example was a perfect example of what not to do. I am more than happy to accept that.

There is a paragraph from ObjectMentor (Uncle Bob's site) that has an example that combines Connection and DataReader objects (two objects previously living in a modem class then separated out) into a ModemImplementation, but states

However, notice that I have recoupled
the two responsibilities into a single
ModemImplementation class. This is not
desirable, but it may be necessary.
There are often reasons, having to do
with the details of the hardware or
OS, that force us to couple things
that we’d rather not couple. However,
by separating their interfaces we have
decoupled the concepts as far as the
rest of the application is concerned.

We may view the ModemImplementation
class is a kludge, or a wart; however,
notice that all dependencies flow away
from it. Nobody need depend upon this
class. Nobody except main needs to
know that it exists. Thus, we’ve put
the ugly bit behind a fence. It’s
ugliness need not leak out and pollute
the rest of the application.

I think the line 'notice that all dependencies flow away from it' is an important one here

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