简体   繁体   English

这是否违反 SRP?

[英]Does this violate SRP?

I often get confused about the "reason to change" or "an axis of change" stated in the book.我经常对书中所说的“改变的理由”或“改变的轴心”感到困惑。

I have a class that makes a character grab a physics object with its hands.我有一个类可以让角色用手抓住一个物理对象。

The grabbable object itself is another class that knows how to get these "grab points" so the grabber class knows where to put the character's hands.可抓取对象本身是另一个知道如何获取这些“抓取点”的类,因此抓取器类知道将角色的手放在哪里。

I think this looks right since each class has distinct responsibilities.我认为这看起来是对的,因为每个班级都有不同的职责。

But they are so coupled that it almost makes sense for them to be one class only.但是它们是如此耦合,以至于它们只成为一个类几乎是有意义的。 Would that break SRP since now the class scans for grab points and also moves/attaches the hands of the character to the object, while still being used cohesively?这会破坏 SRP,因为现在该类扫描抓取点并将角色的手移动/附加到对象上,同时仍被连贯地使用?

The SRP does not tell you to divide classes when they do more than "one thing". SRP 不会告诉您在他们做的不止“一件事”时划分班级。 That's ambiguous, anyway, since programs are functional hierarchies -- all the "things" that programs do are groups of smaller things.无论如何,这是模棱两可的,因为程序是功能层次结构——程序所做的所有“事情”都是一组更小的事情。

The SRP tells you to divide classes when they have more than one line of responsibility , like a worker with two bosses. SRP 告诉您当他们有多个职责时进行划分,例如一个工人有两个老板。 That causes problems, because one boss' instructions might be at odds with what the other boss wants.这会导致问题,因为一位老板的指示可能与另一位老板的要求不一致。

In your case, it makes sense that you would want a class that handles the grabbing action.在您的情况下,您需要一个处理抓取动作的类是有道理的。 This is the class that you modify when you want to change how grabbing works -- that's its responsibility.当您想更改抓取的工作方式时,您可以修改这个类——这是它的职责。

It also makes sense that you should have a class for each kind/type of object, and that class should determine the object's grab points, along with all the other attributes of the object like its shape.同样有意义的是,您应该为每种类型/类型的对象设置一个类,并且该类应该确定对象的抓取点,以及对象的所有其他属性,例如其形状。 This is the class that you modify when you want to change the object(s) -- that's its responsibility.这是您在想要更改对象时修改的类——这是它的责任。

Do you need a separate scanning class that scans the object definition for grab points?您是否需要一个单独的扫描类来扫描对象定义的抓取点? Probably not.可能不是。 I would normally expect the object class to have a method that returns its grab points.我通常希望对象类有一个返回其抓取点的方法。 Following the other SOLID principles, there should probably be a separate Grabbable interface that the object class must implement in order to be grabbable, and this interface would define the method that returns grab points.遵循其他 SOLID 原则,可能应该有一个单独的Grabbable接口,对象类必须实现该接口才能被抓取,并且该接口将定义返回抓取点的方法。

So hopefully you understand what SRP means now, but bear in mind that there is no best way to divide up your program and determine these lines of responsibility.所以希望您现在理解 SRP 的含义,但请记住,没有最好的方法来划分您的程序并确定这些责任线。 Deciding how to do this in a way that best meets all of your functional and organizational requirements is mostly what software design is , and it isn't easy.决定如何以最能满足您的所有功能和组织要求的方式来做这件事是软件设计的主要内容,而且这并不容易。

In particular, if you are making a game, then investigate the Entity, Component, System pattern.特别是,如果您正在制作游戏,请研究实体、组件、系统模式。 This is a very popular way to structure game implementations that is likely to produce a completely different decomposition from what you had in mind.这是一种非常流行的构建游戏实现的方法,它可能会产生与您所想的完全不同的分解。

It is really hard to say whether the classes can be merged into one, however, for the first glance, it looks like you have two responsibilities such as:很难说这些类是否可以合并为一个,但是,乍一看,您似乎有两个职责,例如:

  • scans for grab points扫描抓取点
  • moves/attaches the hands of the character to the object将角色的手移动/连接到对象上

So I would stick with two classes.所以我会坚持两节课。 Because if we stick with just one class, then this class will have two reasons to be changed, debugged or edited.因为如果我们只坚持一个类,那么这个类将有两个原因需要更改、调试或编辑。 So, as a result it will be easier to write unit tests later for your if your classes will be smaller and just have one goal.因此,如果你的类更小并且只有一个目标,那么以后为你编写单元测试会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM