gpt4 book ai didi

solid-principles - SOLID - SRP,一份工作或一个改变的原因

转载 作者:行者123 更新时间:2023-12-05 03:58:51 25 4
gpt4 key购买 nike

<分区>

互联网上有很多关于 SRP 的混淆。

SRP 是否要求:

  1. 类/函数应该做一份工作?
  2. 类/函数应该只有一个改变的原因(我们不不关心我们的类(class)正在执行多少工作,至少当我们考虑 SRP)

例如。

假设我们有一个类执行大量工作(我知道这很糟糕,我们不应该把所有东西都放在一个类中)

此外,我们假设这个类提供一个功能,并且该功能只有一个更改原因,即更改原因只能来自一个参与者(例如我们的 CTO)

此代码是否仍适用于 SRP?

另外引用 Robert C. Martin 的 Clean Architecture

The SOLID principles, the Single Responsibility Principle (SRP) might be the least well understood. That’s likely because it has a particularly inappropriate name. It is too easy for programmers to hear the name and then assume that it means that every module should do just one thing.

Make no mistake, there is a principle like that. A function should do one, and onlyone, thing. We use that principle when we are refactoring large functions intosmaller functions; we use it at the lowest levels. But it is not one of the SOLID principles — it is not the SRP.

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com