恶魔法和平移法

编程入门 行业动态 更新时间:2024-10-25 01:32:37
本文介绍了恶魔法和平移法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这个游戏的源代码是开源的,所以我决定检查一下。在它,我发现了像:

//这个ActionManager基本上是一个控制器,就像在MVC模式。 void ActionManager :: HandleQueryMessage(csString xml,Client * client) { //检查两只手作为开始。 psItem * item = client-> GetCharacterData() - > Inventory()。GetInventoryItem(PSCHARACTER_SLOT_RIGHTHAND); if(!item ||!item-> GetItemCommand()。Length()) item = client-> GetCharacterData() - > Inventory()。GetInventoryItem(PSCHARACTER_SLOT_LEFTHAND); }

获得该项目的第一行明显违反了demeter的定律。然而,即使它改为 client-> GetCharacterData() - > GetInventoryItem(PSCHARACTER_SLOT_RIGHTHAND); ,它仍然违反demeter的定律知道)。

可以做什么?

移动 GetInventoryItem 直到客户端类在我的观点是没有意义的,因为客户端与字符无关。。

在客户端中为所有xx方法创建包装器

解决方案

<

正如你建议自己,如果你想完全关注LOD, > Item * Client :: GetCharacterInventoryItem(int itemID) { return characterData-> getInventoryItem(itemId); } / * ... * / Item * CharacterData :: getInventoryItem(int itemID) { return inventory-> getItem(itemId)} / * ... * / Item * Inventory :: getItem(int itemID) { assert_valid_itemID(itemID); return inventory_table [itemId]; }

这个额外的间接是否值得呢?我不知道,这取决于情况,你的个人喜好等。正如意见指出,LOD应该被视为一个指南,而不是一个真正的法律。另一方面,在我个人的经验,经常打破,你会陷入麻烦...:)

The source code of this game is open source, so I decided to check it out. In it, I found something like:

// This ActionManager is basically a controller like in the MVC pattern. void ActionManager::HandleQueryMessage(csString xml, Client* client) { //check the two hands as a start. psItem* item = client->GetCharacterData()->Inventory().GetInventoryItem(PSCHARACTER_SLOT_RIGHTHAND); if(!item || !item->GetItemCommand().Length()) item = client->GetCharacterData()->Inventory().GetInventoryItem(PSCHARACTER_SLOT_LEFTHAND); }

The first line to get the item clearly violates the law of demeter. However, even if it was changed to client->GetCharacterData()->GetInventoryItem(PSCHARACTER_SLOT_RIGHTHAND);, it would still violate the law of demeter (as far as I know).

What can be done about it? or is this one of the places where LOD doesn't apply [as in my second example]?

Moving the GetInventoryItem up to the client class doesn't makes sense in my point of view since the client has nothing to do with the character.

Creating wrappers in the client class for all the xx methods the character class seems overkill.

Any thoughts?

解决方案

As you suggest yourself, if you want to follow LOD completely, you'd need functions like...

Item* Client::GetCharacterInventoryItem(int itemID) { return characterData->getInventoryItem(itemId); } /* ... */ Item* CharacterData::getInventoryItem(int itemID) { return inventory->getItem(itemId) } /* ... */ Item* Inventory::getItem(int itemID) { assert_valid_itemID(itemID); return inventory_table[itemId]; }

Is this additional indirection worth it? I don't know, that depends on the case, your personal preference, etc. As the comments indicate, the LOD should be viewed as a guideline, not really a law. On the other hand, in my personal experience, break it frequently and you will get into trouble... :)

更多推荐

恶魔法和平移法

本文发布于:2023-11-27 03:01:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:魔法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!