使用getter/setter与“告诉,不要问"? [英] Using getter/setter vs "tell, don't ask"?

查看:82
本文介绍了使用getter/setter与“告诉,不要问"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

告诉我,不要问原则这里经常在我使用时粘贴到我身上吸气剂或二传手,人们告诉我不要使用它们. 该站点清楚地说明了我应该做和不应该做的事情,但是并没有真正解释为什么我应该告诉而不是询问. 我发现使用getter和setter的效率更高,并且我可以使用它们做更多的事情.

Tell, don't ask principle here is often pasted to me when I use getters or setters, and people tell me not to use them. The site clearly explains what I should and what I shouldn't do, but it doesn't really explain WHY I should tell, instead of asking. I find using getters and setters much more efficient, and I can do more with them.

想象一个具有属性healtharmor的类Warrior:

Imagine a class Warrior with attributes health and armor:

class Warrior {
    unsigned int m_health;
    unsigned int m_armor;
};

现在有人通过特殊攻击来攻击我的战士,使他的护甲降低5秒.使用setter是这样的:

Now someone attacks my warrior with a special attack that reduces his armor for 5 seconds. Using setter's it would be like this:

void Attacker::attack(Warrior *target)
{
    target->setHealth(target->getHealth() - m_damage);
    target->setArmor(target->getArmor() - 20);
    // wait 5 seconds
    target->setArmor(target->getArmor() + 20);
}

使用 tell,不要问原理,它看起来像这样(如果我错了,请纠正我):

And with tell, don't ask principle it would look like this (correct me if i'm wrong):

void Attacker::attack(Warrior *target)
{
    target->hurt(m_damage);
    target->reduceArmor(20);
    // wait 5 seconds
    target->increaseArmor(20);
}

现在第二个显然更好,但是我找不到真正的好处. 您仍然需要相同数量的方法(increase/decreaseset/get),并且您失去了询问是否需要询问的好处. 例如,您如何将战士的生命值设置为100? 您如何确定应该使用heal还是hurt,以及需要多少健康才能治愈或受伤?

Now the second one obviously looks better, but I can't find the real benefits of this. You still need the same amount of methods (increase/decrease vs set/get) and you lose the benefit of asking if you ever need to ask. For example, how would you set warriors health to 100? How do you figure out whether you should use heal or hurt, and how much health you need to heal or hurt?

此外,我看到世界上一些最好的程序员正在使用setter和getter. 大多数API都使用它,并且一直在std lib中使用它:

Also, I see setters and getters being used by some of the best programmers in the world. Most APIs use it, and it's being used in the std lib all the time:

for (i = 0; i < vector.size(); i++) {
    my_func(i);
}
// vs.
vector.execForElements(my_func);

如果我必须决定是否要相信这里的人将我的一篇有关讲述而不是发问的文章链接到我,还是要相信90%的大公司(苹果,微软,android,大多数游戏等),他们已经成功赚了很多钱,并开发了很多工作程序,这让我很难理解为什么告诉,不问是一个好原则.

And if I have to decide whether to believe people here linking me one article about telling, not asking, or to believe 90% of the large companies (apple, microsoft, android, most of the games, etc. etc.) who have successfully made a lot of money and working programs, it's kinda hard for me to understand why would tell, don't ask be a good principle.

在使用getter和setter看起来似乎更容易时,为什么我应该使用它(应该?)?

Why should I use it (should I?) when everything seems easier with getters and setters?

推荐答案

您仍然需要使用相同数量的方法(增加/减少与设置/获取),并且失去了询问是否需要询问的好处.

You still need the same amount of methods (increase/decrease vs set/get) and you lose the benefit of asking if you ever need to ask.

您理解错了.关键是要用有意义的操作替换getVariablesetVariable:例如,inflictDamage.将getVariable替换为increaseVariable只会为getter和setter提供其他更晦涩的名称.

You got it wrong. The point is to replace the getVariable and setVariable with a meaningful operation: inflictDamage, for example. Replacing getVariable with increaseVariable just gives you different more obscure names for the getter and setter.

这件事在哪里.例如,您不需要提供设置器/获取器来以不同的方式跟踪装甲和生命值,则班级可以通过尝试阻止(并在此过程中损坏防护罩)来处理单个inflictDamage如果盾牌不够或您的算法要求,请在字符上输入.同时,您可以在一个地方添加更复杂的逻辑.

Where does this matter. For example, you don't need to provide a setter/getter to track the armor and health differently, a single inflictDamage can be processed by the class by trying to block (and damaging the shield in the process) and then taking damage on the character if the shield is not sufficient or your algorithm demands it. At the same time you can add more complex logic in a single place.

添加一个魔法盾牌,例如,在受到伤害时,它会在短时间内暂时增加武器造成的伤害.如果您有getter/setter,则所有攻击者都需要查看您是否有这样的物品,然后在多个地方应用相同的逻辑,以期获得相同的结果.在 tell 方法中,攻击者仍需要仅弄清楚他们造成的伤害,并将其告知角色.然后,角色可以弄清楚损坏是如何分布在物品上的,以及是否以其他任何方式影响角色.

Add a magic shield that will temporarily increase the damage caused by your weapons for a short time when taking damage, for example. If you have getter/setters all attackers need to see if you have such an item, then apply the same logic in multiple places to hopefully get to the same result. In the tell approach attackers still need to just figure out how much damage they do, and tell it to your character. The character can then figure out how the damage is spread across the items, and whether it affects the character in any other way.

使游戏复杂化并添加射击武器,则可以使用inflictFireDamage(或将射击伤害作为不同的参数传递给inflictDamage函数). Warrior可以确定她是否受到耐火咒语的影响而忽略了火焰伤害,而不必让程序中的所有其他对象尝试弄清楚她的行为将如何影响其他对象.

Complicate the game and add fire weapons, then you can have inflictFireDamage (or pass the fire damage as a different argument to the inflictDamage function). The Warrior can figure out whether she is affected by a fire resistance spell and ignore the fire damage, rather than having all other objects in the program try to figure out how their action is going to affect the others.

这篇关于使用getter/setter与“告诉,不要问"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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