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.
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.
Let's examine a scenario involving a sensor's value and an alarm triggered when this value exceeds a threshold.
<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>
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>
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>
Usage:
<code>TellMonitor monitor = new TellMonitor("Temperature Sensor", 100, alarm); monitor.setValue(120);</code>
The "Tell" version eliminates external decision-making by encapsulating the logic within the setValue
method.
✅ 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.
✅ 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.
✅ 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 aServer
's load for traffic management.
Interested? ? Explore other posts in my programming principles series!
Stay updated on future posts:
The above is the detailed content of 'Tell, Dont Ask' Principle Explained in Seconds. For more information, please follow other related articles on the PHP Chinese website!