本文介绍了恶魔法和平移法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这个游戏的源代码是开源的,所以我决定检查一下。在它,我发现了像: //这个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... :) 这篇关于恶魔法和平移法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-02 00:53