1

For example, I have a clan and a character. There's a character that is the leader. To give the clan a specific feature, some money from the character is required.

I don't want to have too much tight coupling. Right now I have a member in the clan class like this:

bool clan::give_rank(character* chr, int rank)
{ 
    if (!is_leader(chr.id()) || !chr->has_money(500))
       return false;

    this->rank_ = rank;
    chr->take_money(500); 
    return true;
}

Is this tight coupling? or maybe I should have a secondary class like a clan_mgr that connects both classes?

bool clan_mngr::give_rank(character* chr, int rank)
{ 
    clan* myclan = chr->get_clan(); 

    if (!myclan || !myclan->is_leader(chr.id()) || !chr->has_money(500))
       return false;

    myclan->rank_ = rank;
    chr->take_money(500);
    return true;
}

// Or maybe this one, which looks even worse imo:

bool character::give_rank_to_clan(int rank)
{ 
    clan* myclan = chr->get_clan(); 

    if (!myclan || !myclan->is_leader(id()) || !has_money(500))
       return false;

    myclan->rank_ = rank;
    TakeMoney(500);    
    return true;
}
  • 1
    As a sidenote you should move the has_money into the TakeMoney method (or a separate PayMoney method) if (PayMoney(500)) SetRank – Alfons Jun 16 '16 at 9:58
1

Your first approach look good. Clan/character is a container relashionship and so the ideal approach would be the Clan has a list of members, and the character is unaware of Clan class. That means character's API must be designed so Clan can implement its services.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.