The law of demeter and planetary

The source code for 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 element clearly violates the demeter law. However, even if it was changed to client->GetCharacterData()->GetInventoryItem(PSCHARACTER_SLOT_RIGHTHAND);, it will still violate the demeter law (as far as I know).

What can be done about this? or is this one of the places where LOD is not applied [as in my second example]?

Moving a class GetInventoryItemto a class clientdoes not make sense in my point of view, since the client has nothing to do with character.

Creating wrappers in the class clientfor all xx methods the class characterseems redundant.

Any thoughts?

+1
1

, 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 , . , , , ...:)

0

Source: https://habr.com/ru/post/1619158/


All Articles