Home > Backend Development > C++ > 'Tell, Don&#t Ask' Principle Explained in Seconds

'Tell, Don&#t Ask' Principle Explained in Seconds

Linda Hamilton
Release: 2025-01-19 10:03:10
Original
476 people have browsed it

The Tell, Don't Ask principle, a cornerstone of object-oriented programming (OOP), advocates for designing objects that encapsulate both their data and the methods that manipulate that data. This approach fosters more maintainable and robust systems by enhancing encapsulation.


? Understanding "Tell, Don’t Ask"

The "Tell, Don't Ask" principle emphasizes instructing objects on what to do, rather than retrieving their data and processing it externally. It promotes keeping logic and state within the object itself.

Instead of accessing an object's data to make external decisions, you directly instruct the object to perform an internal action. This strategy simplifies code, minimizes dependencies, and improves system extensibility and maintainability.


?‍?‍? Example: Sensor Value Monitoring

Let's examine a scenario involving a sensor's value and an alarm triggered when this value exceeds a threshold.

The "Ask" Approach

<code>class AskMonitor {
  private int value;
  private int limit;
  private String name;
  private Alarm alarm;

  public AskMonitor(String name, int limit, Alarm alarm) {
    this.name = name;
    this.limit = limit;
    this.alarm = alarm;
  }

  public int getValue() { return value; }
  public void setValue(int value) { this.value = value; }
  public int getLimit() { return limit; }
  public String getName() { return name; }
  public Alarm getAlarm() { return alarm; }
}</code>
Copy after login

Usage:

<code>AskMonitor monitor = new AskMonitor("Temperature Sensor", 100, alarm);
monitor.setValue(120);

if (monitor.getValue() > monitor.getLimit()) {
  monitor.getAlarm().warn(monitor.getName() + " is too high");
}</code>
Copy after login

The "Tell" Approach

With "Tell, Don't Ask," the behavior is integrated into the Monitor class.

<code>class TellMonitor {
  private int value;
  private int limit;
  private String name;
  private Alarm alarm;

  public TellMonitor(String name, int limit, Alarm alarm) {
    this.name = name;
    this.limit = limit;
    this.alarm = alarm;
  }

  public void setValue(int value) {
    this.value = value;
    if (this.value > this.limit) {
      alarm.warn(name + " is too high");
    }
  }
}</code>
Copy after login

Usage:

<code>TellMonitor monitor = new TellMonitor("Temperature Sensor", 100, alarm);
monitor.setValue(120);</code>
Copy after login

The "Tell" version eliminates external decision-making by encapsulating the logic within the setValue method.


⭐ Advantages of "Tell, Don't Ask"

Stronger Encapsulation: Data and behavior are tightly coupled.
Concise Code: External logic is minimized by internalizing behavior.
Improved Maintainability: Easier to modify and extend functionality.


? When to "Tell"

Encapsulated Behavior: When an object inherently knows how to process its data.

Example: A Monitor object, aware of its limit, should autonomously trigger an alarm upon exceeding that limit.

State-Triggered Actions: When state changes necessitate subsequent actions (e.g., notifications, logging).

Example: A UserProfile object automatically updates an activity log upon profile modifications.


? When to "Ask"

Data Retrieval: When data is needed without altering the object's state.

Example: Retrieving a User object's email address.

External Decision-Making: When decisions rely on external factors.

Example: Obtaining a Person's name to externally determine an appropriate greeting.

Responsibility Delegation: When objects collaborate, and one requires data from another for decision-making.

Example: A Router querying a Server's load for traffic management.


? Related Resources

Interested? ? Explore other posts in my programming principles series!

  • KISS Design Principle Explained in 100 Seconds
  • DRY Principle Explained in 100 Seconds

Stay updated on future posts:

  • Linkedin
  • Github
  • Twitter/ X

The above is the detailed content of 'Tell, Don&#t Ask' Principle Explained in Seconds. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template