本文介绍了在使用单一责任原则时,您如何确定粗糙或细粒度的“责任”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SRP中,责任通常被描述为改变的原因,所以每个类(或对象?)应该只有一个原因应该在那里进行改变。



但是,如果你把这个细微粒子,你可以说一个将两个数字加在一起的对象是一个责任和一个可能的改变原因。因此,对象不应该包含任何其他逻辑,因为它会产生另一个改变的原因。



如果有任何人有任何策略,我很好奇, ',单一责任原则稍微不那么客观?

解决方案

它归结于你正在建模的上下文。我在SOLID原则上做了一些广泛的写作和介绍,我在讨论单一责任问题时就专门处理了你的问题。



以下是在Jan / Feb 2010年版Code Magazine,可通过







In the SRP, a 'responsibility' is usually described as 'a reason to change', so that each class (or object?) should have only one reason someone should have to go in there and change it.

But if you take this to the extreme fine-grain you could say that an object adding two numbers together is a responsibility and a possible reason to change. Therefore the object should contain no other logic, because it would produce another reason for change.

I'm curious if there is anyone out there that has any strategies for 'scoping', the single-responsibility principle that's slightly less objective?

解决方案

it comes down to the context of what you are modeling. I've done some extensive writing and presenting on the SOLID principles and I specifically address your question in my discussions of Single Responsibility.

The following first appeared in the Jan/Feb 2010 issue of Code Magazine, and is available online at "S.O.L.I.D. Software Development, One Step at a Time"


这篇关于在使用单一责任原则时,您如何确定粗糙或细粒度的“责任”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:22