不是信息专家告诉“不要单问责任原则"吗? [英] Aren't Information Expert & Tell Don't Ask at odds with Single Responsibility Principle?

查看:62
本文介绍了不是信息专家告诉“不要单问责任原则"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Information-Expert 告诉-不要问 SRP 称为最佳实践.但是我认为他们是矛盾的.这就是我在说的.

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 talking about.

支持SRP但违反了Tell-Don't-Ask&的代码信息专家:

Code that favors SRP but violates Tell-Don't-Ask & Info-Expert:

Customer bob = ...;
// TransferObjectFactory has to use Customer's accessors to do its work, 
// violates Tell Don't Ask
CustomerDTO dto = TransferObjectFactory.createFrom(bob); 

支持Tell-Don't-Ask&的代码信息专家,但违反了SRP:

Code that favors Tell-Don't-Ask & Info-Expert but violates SRP:

Customer bob = ...;
// Now Customer is doing more than just representing the domain concept of Customer,
// violates SRP
CustomerDTO dto = bob.toDTO();

请向我介绍这些做法如何和平共处.

Please fill me in on how these practices can co-exist peacefully.

术语的定义,

  • Information Expert:具有操作所需数据的对象应托管该操作.

  • Information Expert: objects that have the data needed for an operation should host the operation.

告诉不要问:不要为了工作而向对象索要数据.告诉对象做这项工作.

Tell Don't Ask: don't ask objects for data in order to do work; tell the objects to do the work.

单一责任原则:每个对象都应具有狭义的责任.

Single Responsibility Principle: each object should have a narrowly defined responsibility.

推荐答案

我认为它们并没有强调太多会引起您痛苦的东西,因此并没有太大的分歧.一种是关于结构化代码以弄清特定职责在哪里并减少耦合,另一种是关于减少修改类的原因.

I don't think that they are so much at odds as they are emphasizing different things that will cause you pain. One is about structuring code to make it clear where particular responsibilities are and reducing coupling, the other is about reducing the reasons to modify a class.

我们每个人每天都必须做出有关如何构造代码以及我们愿意在设计中引入哪些依赖性的决策.

We all have to make decisions each and every day about how to structure code and what dependencies we are willing to introduce into designs.

我们已经建立了许多有用的准则,准则和模式,可以帮助我们做出决策.

We have built up a lot of useful guidelines, maxims and patterns that can help us to make the decisions.

这些中的每一个对于检测我们的设计中可能存在的不同类型的问题都是有用的.对于您可能要解决的任何特定问题,某个地方都会有一个最佳解决方案.

Each of these is useful to detect different kinds of problems that could be present in our designs. For any specific problem that you may be looking at there will be a sweet spot somewhere.

不同的准则确实相互矛盾.仅应用您所听到或阅读的每条指南都不会使您的设计更好.

The different guidelines do contradict each other. Just applying every piece of guidance you have heard or read will not make your design better.

对于您今天要解决的特定问题,您需要确定可能导致您痛苦的最重要因素是什么.

For the specific problem you are looking at today you need to decide what the most important factors that are likely to cause you pain are.

这篇关于不是信息专家告诉“不要单问责任原则"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆