Canonical: https://weaveos.com/zh-cn/glossary/tell-dont-ask

[首页](https://weaveos.com/zh-cn)/[术语表](https://weaveos.com/zh-cn/glossary)/[Code quality and technical debt](https://weaveos.com/zh-cn/glossary/category/code-quality-technical-debt)/告诉而非询问 (Tell-don’t-ask)

[Code quality and technical debt](https://weaveos.com/zh-cn/glossary/category/code-quality-technical-debt)

# 告诉而非询问 (Tell-don’t-ask)

也称为 告诉而非询问 (Tell-don’t-ask), 软件工程中的“告诉而非询问”（Tell-don’t-ask）

作者：Weave发布于 2026年9月10日1 分钟阅读

## 定义

一种设计准则，主张要求对象执行行为，而不是提取其状态并在别处进行决策。

在本页中

1. [定义](https://weaveos.com/zh-cn/glossary/tell-dont-ask#definition)
2. [它的含义](https://weaveos.com/zh-cn/glossary/tell-dont-ask#article-section)
3. [示例](https://weaveos.com/zh-cn/glossary/tell-dont-ask#article-section-2)
4. [局限性](https://weaveos.com/zh-cn/glossary/tell-dont-ask#article-section-3)
5. [如何使用该信号](https://weaveos.com/zh-cn/glossary/tell-dont-ask#article-section-4)
6. [这与 Weave 有何关系](https://weaveos.com/zh-cn/glossary/tell-dont-ask#weave)
7. [来源](https://weaveos.com/zh-cn/glossary/tell-dont-ask#sources)
8. [相关术语](https://weaveos.com/zh-cn/glossary/tell-dont-ask#related)

## 它的含义

“告诉而非询问”（Tell-don’t-ask）鼓励行为与它所管理的数据和不变量共存。调用者可以发送有意义的命令，而不是读取对象的字段、在另一个类中进行分支并写回结果。这可以减少重复的规则并保持所有权清晰。

## 示例

结账流程会向账户查询余额、检查限额并在外部改变状态。将决策移至账户操作中，可以让账户一致地强制执行不变量。

## 局限性

如果将每个决策都推入一个模型，该准则可能会创建过于庞大的对象。当调用者真正拥有策略或需要投影时，查询操作是合适的。请根据责任和变更所有权来进行选择。

## 如何使用该信号

当调用者真正拥有该策略时，请保留查询行为，因为并非每次读取都是错位的责任。

Weave 可以将行为改变的重构与审查和返工模式联系起来。它无法决定领域所有权，因此请将该信号视为设计审查的提示。

## 这与 Weave 有何关系

Weave 可以将行为改变的重构与审查和返工模式联系起来。它无法决定领域所有权，因此请将该信号视为设计审查的提示。

[探索 工程智能](https://weaveos.com/zh-cn/products/engineering-intelligence)

## 来源与延伸阅读

1. [SonarSource code metrics and analysis documentation](https://docs.sonarsource.com/sonarqube-server/latest/user-guide/code-metrics/metrics-definition/)

## 相关术语

### [开发者生产力](https://weaveos.com/zh-cn/glossary/developer-productivity)

[了解开发者生产力，为何活动计数是不完整的，以及如何评估交付、质量和人工智能辅助的工程工作。](https://weaveos.com/zh-cn/glossary/developer-productivity)

### [技术债务](https://weaveos.com/zh-cn/glossary/technical-debt)

[技术债务的实用定义，它如何产生未来的工程成本，以及如何决定哪些债务值得关注。](https://weaveos.com/zh-cn/glossary/technical-debt)
