Giriş
Bu kural 3 varsayım ile geliyor. Bunlar şöyle.
Şu kod kuralı ihlal eder.
Bu kural 3 varsayım ile geliyor. Bunlar şöyle.
1. You're using objects.Tell, Don't Ask kuralı başka nesnenin iç durumuna erişme diyor. Açıklaması şöyle.
2. Your objects have state.
3. The state of your objects affects their behavior.
Bir başka açıklama şöyle.Allow your class to manage its own state. Don't ask it for its state, and then take action based on that state. Tell the class what you want, and let it decide what to do based on its own state.
ÖrnekTell-Don't-Ask is a principle that helps people remember that object-orientation is about bundling data with the functions that operate on that data. It reminds us that rather than asking an object for data and acting on that data, we should instead tell an object what to do. This encourages us to move behavior into an object to go with the data.
Şu kod kuralı ihlal eder.
var color = trafficLight.Color;
var elapsed = trafficLight.Elapsed;
If (color == Color.Red && elapsed > 2.Minutes)
trafficLight.ChangeColor(green);
Düzeltmek için şöyle yaparız.var result = trafficLight.ChangeColor(Color.Green);
Hiç yorum yok:
Yorum Gönder