Aren't Information Expert & Tell Don't Ask at odds with Single Responsibility Principle?

前端 未结 6 1536
没有蜡笔的小新
没有蜡笔的小新 2020-12-25 12:36

Information-Expert, Tell-Don\'t-Ask, and SRP are often mentioned together as best practices. But I think they are at odds. Here is what I\'m talki

6条回答
  •  一个人的身影
    2020-12-25 13:18

    I don't 100% agree w/ your two examples as being representative, but from a general perspective you seem to be reasoning from the assumption of two objects and only two objects.

    If you separate the problem out further and create one (or more) specialized objects to take on the individual responsibilities you have, and then have the controlling object pass instances of the other objects it is using to the specialized objects you have carved off, you should be able to observe a happy compromise between SRP (each responsibility has handled by a specialized object), and Tell Don't Ask (the controlling object is telling the specialized objects it is composing together to do whatever it is that they do, to each other).

    It's a composition solution that relies on a controller of some sort to coordinate and delegate between other objects without getting mired in their internal details.

提交回复
热议问题