What exactly is modular coding, and am I doing it right?
<p>I'm trying to figure out if I understand correctly what modular code really is. I know that it's extremely powerful / useful so I want to make sure I'm learning it correctly.</p>

<p>To start learning, I made a fairly simple system that groups Entities together based on some criteria. First come, first serve.</p>

<p>I also want to graphically display the groups and their member's, to show the user (myself) what's going on. I'll use the SDL library for this.</p>

<p>So let's divide that into some classes;</p>

<p><strong>Entities</strong></p>

<p>The main class "Entities" will have a Boost::mutex for thread safety, and a map holding pointers to actual entities. Let's also make a function that retrieves a pointer to the actual entity from said map, when provided with the entity ID.</p>

<pre><code>class Entities
{
public:
    boost::mutex access_entitymap; //Thread safety
    std::map&lt;unsigned int, Entity*&gt; entitymap;

    Entity* getEntity(unsigned int entityID);
};
</code></pre>

<p><strong>Entity</strong></p>

<p>Holds information about the actual entity, such as it's uniqueue ID. And a pointer to the parent "Entities".</p>

<p><strong>LFDGroups</strong></p>

<p>The grouping system. 
Includes a map with pointers to the actual groups, 
a map with the group's position in the queue (first come first serve. full groups are removed from this map to make iterating faster), 
and a map with an entity's position in the group (5 spots, which entity has taken which spot?).
Furthermore it also includes some functions to operate on said maps. Such as adding and removing members. The <code>addmember()</code> function will, as an example, make sure the entity is added to the group with the most priority, or that a new group is made if necessary.</p>

<p><strong>groupObject</strong></p>

<p>Holds information about the actual group, such as it's uniqueue ID and it's inhabitant entities' IDs. But also has some functions such as;</p>

<pre><code>LFDGroups *lfdgroup; //Parent LFDGroups;
bool isEmpty(); //is this group empty?
bool isFull(); //is this group full?
unsigned int getPosition(); //Position in queue?
bool addMember(unsigned int entityID,bool snatchIt); 
/*Add a member. Calls removeMember() on the entities' groupObject 
first if the entityID is already in another group.*/
bool removeMember(unsigned int entityID,bool snatchIt); 
/*Remove a member from this group, and snatch a member from another 
group further up in the queue. This ensures groups are always full 
if enough people are queued.*/
bool hasMember(unsigned int entityID); //Is this entity ID in our group?
/*etc. etc.*/
</code></pre>

<p><strong>Queue</strong></p>

<p>Provides some ease-of-use functions to make testing easier. Such as;</p>

<pre><code>void addFakeEntity(bool queuenow); //Create an all new entity and immediately queue it.
void removeFakeEntity(); //Remove a random entity from a random group
void removeFakeEntity(unsigned int entityID); //Remove a given entity from it's corresponding group.
</code></pre>

<p><strong>SDLMain</strong></p>

<p>Graphically displays what's going on with LFDGroups' groups. How many members it has, which spots are taken and which aren't etc. </p>

<p>Also proves buttons to work with Queue's functions. Such as the "Add" button will call "queue->addFakeEntity(true)"</p>

<p>So now I can just start the whole thing up easily;</p>

<pre><code>#include "SDL_main.h" //gfx
#include "class_LFDGroups.h" //group system
#include "queue.h" //ease of use for testing
#include "class_entities.h" //entities

int main( int argc, char* args[] )
{
    Entities * entities = new Entities;
    LFDGroups * lfdgroups = new LFDGroups(entities);
    Queue * queue = new Queue(lfdgroups);
    SDLMain * sdlmain = new SDLMain(queue);
    return 1;
}
</code></pre>

<ol>
<li>Entities can be used without anything else.</li>
<li>LFDGroups can be used with nothing but entities.</li>
<li>Queue needs both, but LFDGroups already contains a pointer to
Entities. After all, it can't function without it.</li>
<li>SDLMain uses all 3.</li>
</ol>

<p>And sure enough;</p>

<p><img src="http://i.stack.imgur.com/ejsES.png" alt="group_example"></p>

<p>So this means that even if I do disable the graphically displaying of the groups, I can still use the actual grouping system for other things. 
And also, functions relating to certain objects are within that object itself. As an example, the <code>Entity* getEntity(unsigned int entityID);</code> function is in the <code>Entities</code> class and not in <code>groupObject</code>, even though that is the only one that's using it at the moment.</p>

<p>Is this all that modular coding is? Am I doing it right?</p>
